Sunday, November 15, 2009

More on VIM syntax highlighting

To force a syntax scheme of one language on files in other languages or those with no file extension.

In vim editor, key in:
:setf [language _name]

For example, to force a perl syntax on a *.go file; in the vim editor, key in:
:setf perl

To have this behavior by default, modify the filetype.vim file.

benoy@palazhi:~$ find / -name 'filetype.vim' 2>/dev/null
/usr/share/vim/vim71/filetype.vim

benoy@palazhi:~$ sudo vim /usr/share/vim/vim71/filetype.vim
[sudo] password for benoy:

After typing the password, include the following line in the file:

" Go project file
au BufNewFile,BufRead *.go setf perl

This will ensure that all *.go files are shown with perl syntax color highlighting by default in vim.

[NOTE]
Ensure that there is no space between the strings "BufNewFile", "," & "BufRead" in the above line.
[/NOTE]

Of course, it is better to have the Go syntax itself on while opening *.go files, instead of the perl syntax scheme. For that do the following:

1. Download go.vim (http://code.google.com/p/go/source/browse/misc/vim/go.vim?r=release)

2. Save it to the ftplugin directory
benoy@palazhi:~$ find / -name 'ftplugin' 2>/dev/null
/usr/share/vim/vim71/ftplugin

3. Add the following lines to .vimrc file

" Syntax highlighting for *.go
autocmd BufNewFile,BufRead *.go source /usr/share/vim/vim71/ftplugin/go.vim

[NOTE]
Ensure that there is no space between the strings "BufNewFile", "," & "BufRead" in the above line.

To locate the vimrc file:
benoy@palazhi:~$ find / -name 'vimrc' 2>/dev/null
/usr/share/vim/vimrc /etc/vim/vimrc
[/NOTE]

Now, *.go files will be shown in the Go syntax scheme by default in the vim editor.

No comments:

Post a Comment

Followers