Saturday, April 19, 2008

VIM syntax highlight

4. Color Syntax init files

4.1 Auto source-in method

This section below is obtained from gvim session by typing 'help syntax' -

bash$ gvim some_test
:help syntax

Click on the menu Window=>Close_Others to close other Window. And then do CTRL+] on 'Syntax Loading Procedure' menu which will take you there. (Use CTRL+T to rewind and go back).

If a file type you want to use is not detected, then there are two ways to add it. Method 1: You can modify the $VIMRUNTIME/filetype.vim file, but this is not recommended as it will be overwritten when you install a new version of Vim.

Method 2: Create a file in $HOME/vim/myfiletypes.vim and put these lines in it -

" Filename : $HOME/vim/myfiletypes.vim
" See the document by typing :help autocmd within vim session
" see also the doc at /usr/share/vim/doc/autocmd.txt
" This file will setup the autocommands for new filetypes
" using the existing syntax-filetypes.
" For example when you open foo.prc it will use syntax of plsql
" Basically does :set filetype=prc inside vim
" Add a line in $HOME/.gvimrc as below:
" so $HOME/vim/myfiletypes.vim

augroup filetype
au! BufRead,BufNewFile *.phc set filetype=php
au! BufRead,BufNewFile *.mine set filetype=mine
au! BufRead,BufNewFile *.xyz set filetype=drawing
au! BufRead,BufNewFile *.prc set filetype=plsql
augroup END

Then add a line in your $HOME/.vimrc and $HOME/.gvimrc file to source in the file "myfiletypes.vim". (CAUTION: You MUST put this in both vimrc and gvimrc files in order for this to work) Example:
        so $HOME/vim/myfiletypes.vim

NOTE: Make sure that you set "so myfiletypes.vim" before switching on file type detection. This is must be before any ":filetype on" or ":syntax on" command.

See the documentation on autocommand at -

  • :help autocmd (within a vim editing session)
  • See also the doc at /usr/share/vim/doc/autocmd.txt

Your file will then be sourced in after the default FileType autocommands have been installed. This allows you to overrule any of the defaults, by using ":au!" to remove any existing FileType autocommands for the same pattern. Only the autocommand to source the scripts.vim file is given later. This makes sure that your autocommands in "myfiletypes.vim" are used before checking the contents of the file.

4.2 Manual method

Instead of using "Syntax" menu you can also manually source in the syntax file. Edit the file with gvim and at : (colon) command give 'so' command. For example -

        gvim foo.pc
:so $VIM/syntax/esqlc.vim

The syntax source files are at /usr/share/vim/syntax/*.vim. Vim supports more than 120 different syntax files for different languages like C++, PERL, VHDL, JavaScript,...and so on!!

Each syntax file supports one or more default file name extensions, for example, JavaScript syntax file supports the *.js extension. If you happen to use an extension that conflicts with another default syntax file (such as adding JavaScript to a *.html file) than you can source in the additional syntax file with the command :so $VIM/syntax/javascript.vim. To avoid all of this typing, you can create a soft link like -

        ln -s $VIM/syntax/javascript.vim js
gvim foo.html (... this file contains javascript functions and HTML)
:so js

No comments: