Keeping it Small and Simple

2008.01.11

Getting scala syntax hightlighting to work in vim

Filed under: Vim — Tags: , , — Lorenzo E. Danielsson @ 13:44

If you want vim to be able to syntax highlight and indent scala source files you can

First of all go here and you will see three sub-directories: ftdetect, indent and syntax.

Each one of the sub-directories contains a file called scala.vim. Download them. Notice that if you save them one after the other they will overwrite one another, so you might want to give them names like scala-ftd.vim, scala-ind.vim and scala-syn.vim.

If you only want your user to have scala filetype support, you can move the files into ~/.vim. Make sure the directories ~/.vim/ftdetect, ~/.vim/indent and ~/.vim/syntax all exist then:

mv scala-ftd.vim ~/.vim/ftdetect/scala.vim
mv scala-ind.vim ~/.vim/indent/scala.vim
mv scala-syn.vim ~/.vim/syntax/scala.vim

This is a good solution if you don’t have root access on the system. If you do and you want all your users to have access to scala filetype detection, do the following instead:

mv scala-ftd.vim /usr/share/vim/addons/ftdetect/scala.vim
mv scala-ind.vim /usr/share/vim/addons/indent/scala.vim
mv scala-syn.vim /usr/share/vim/addons/syntax/scala.vim

What if it doesn’t work?

I grabbed my scala vim files from a different location and they refused to work. Looking at :set output in vim I saw that there was garbage at the end of the filetype name (scala^M instead of scala). If this happens to you you can solve it by converting line-endings from DOS to UNIX. There are about 100,000,000 ways to do this, including writing your own little script in **your favorite language**, but the simplest possible solution might be this:

% su
[enter root password]
# aptitutde install tofrodos
# exit
% cd ~/.vim/ftdetect/scala.vim
% fromdos scala.vim

Make sure you invoke fromdos on the other two scala.vim files as well. Once that is done, scala filetype support should work.

2007.05.14

Quick Vim-tip: getting vim to know .zsh files

Filed under: Vim — Lorenzo E. Danielsson @ 07:09

I’ve been asked this question quite a few times, so I thought I might as well add it here, just in case it helps someone out there..

You may have noticed that Vim supports zsh files, but that files with the extension .zsh don’t get their syntax highlighted by default. That is simple to fix (as with most things in Vim). Simply add this to your ~/.vimrc:


autocmd BufNewFile,BufRead *.zsh setlocal filetype=zsh

Once you have added this, you can try something like vim somefile.zsh. The zsh ftplugin should now be loaded, and you should get your syntax highlighted. This of course assumes that you have enabled syntax highlighting (syntax on) as well as file-type plug-ins (filetype plugin on).

You can use the same principle for other files or file patterns as well. In my vimrc file you will find the following line:


autocmd BufNewFile,BufRead vimoutlinerrc setlocal filetype=vim

because I use the Vim outliner, and vim doesn’t (yet) recognize the file vimoutlinerrc to be a vim file.

An alternative to adding a bunch of autocmds to your vimrc would be to use modelines (see :help modeline if you don’t know what a modeline is). But I personally don’t like to add modelines to my files. Feel free to use which ever solution suits you best.

Blog at WordPress.com.