Friday, July 14, 2006

syntax highlight for non-standard file types

Following line once put into vimrc will tell vim which syntax highlighting to use:

au BufReadPost SCons* set syntax=python
au BufReadPost Cons* set syntax=perl
au BufReadPost *.mke set syntax=make
au BufReadPost make*.inc set syntax=make
au BufReadPost *.fcc set syntax=cpp
au BufReadPost *.fhh set syntax=cpp


This is selfexplanatory. e.g. SCons files are in fact python scripts - so tell vim to use python syntax highlighting on everything what matches 'SCons*'. Same goes for Construct - but done in perl.

:au BufReadPost <mask> tells vim to execute something right after file read into buffer. In that particular case we use that to set buffer local variable 'syntax' to name of one of standard syntax highlightings (perl, make, cpp, etc). :help :au for more info.

P.S. One can check what syntax module vim has loaded by looking at 'syntax' variable value: do :set syntax? after opening file recognized by vim.

P.P.S. Of course, :syntax on has to be put somewhere in the vimrc for syntax highlighting to work automatically. VIM 6.x doesn't have problem of previous versions (I experienced that in VIM 5.x), where loading of syntax from vimrc was slowing launch time, even if file had no syntax highlighting assigned. Hm.. or probably my PC now fast enough for me not to notice that launch time. Who cares. It works.

7 comments:

The MZA said...

Thanks! This is exactlty what I was looking for.

Nate said...

me too! Thanks for sharing.

oc said...

Thanks a lot! very useful

Ningappa said...

Oh! this tip really helped me in highlighting the proc files.....

-Ningappa

Vargas said...

Thanks for the tip! \o/

HSP said...

Lot of thanks

Jason said...

One of the best tips ever; thanks!