Friday, February 18, 2011

Vim Usage: add more syntax to support special extension

Vim is able to detect your programming language in the file you open. The mechanism for vim to do that is to check extension of the file. However, some files are not included by default in vim like .inc. To make vim can correctly detect the syntax with your file extension. You need to set file type explicitly. In command mode, input ":set filetype=php" to tell vim that the current opened file is using php syntax.

However, it is annoying that you need to type this command whenever you open .inc file. To do that permanently, modify filetype.vim under ~/.vim folder which store all vim settings and plugin for current session user. Then, find "au BufNewFile,BufRead *.php,*.php\d,*.phtml,*.ctp    setf php" which mean for the file with extension *.php, *.phtml and *.ctp, vim will treat them as php syntax. Thus, you just need to add *.inc to this line, that becomes "au BufNewFile,BufRead *.php,*.php\d,*.phtml,*.ctp,*.inc    setf php", save and then reopen .inc file. You can see it is using php syntax now.

If you have some syntax you want to diy, you can create a syntax file under ~/.vim/syntax folder. Then in filetype.vim add "au BufNewFile,BufRead *.inc    setf inc". Save it and you will use your own inc syntax format for inc extension files. Nice!

No comments:

Post a Comment