vim has a very powerful sub-language for
defining syntax highlighting. The syntax.txt
help file in vim 5.1 that describes it
is over 1,500 lines long. Therefore, we won't attempt
to give all the details here. Instead, the sample file
below should give you some taste for what vim can
do. The example consists of portions of the syntax file for Awk:
" Vim syntax file
" Language: awk, nawk, gawk, mawk
" Maintainer: Antonio Colombo <antonio.colombo@jrc.org>
" Last change: 1997 November 29
" Remove any old syntax stuff hanging around
syn clear
" A bunch of useful Awk keywords
syn keyword awkStatement break continue delete exit
...
syn keyword awkFunction atan2 close cos exp int log rand sin \
sqrt srand
...
syn keyword awkConditional if else
syn keyword awkRepeat while for do
syn keyword awkPatterns BEGIN END
syn keyword awkVariables ARGC ARGV FILENAME FNR FS NF NR
...
" Octal format character.
syn match awkSpecialCharacter contained "\\[0-7]\{1,3\}"
" Hex format character.
syn match awkSpecialCharacter contained "\\x[0-9A-Fa-f]\+"
syn match awkFieldVars "\$[0-9]\+"
syn match awkCharClass contained "\[:[^:\]]*:\]"
syn match awkRegExp contained "/\^"ms=s+1
syn match awkRegExp contained "\$/"me=e-1
syn match awkRegExp contained "[?.*{}|+]"
...
" Numbers, allowing signs (both -, and +)
" Integer number.
syn match awkNumber "[+-]\=\<[0-9]\+\>"
" Floating point number.
syn match awkFloat "[+-]\=\<[0-9]\+\.[0-9]+\>"
...
syn match awkComment "#.*" contains=awkTodo
if !exists("did_awk_syntax_inits")
let did_awk_syntax_inits = 1
" The default methods for highlighting. Can be overridden later
hi link awkConditional Conditional
hi link awkFunction Function
hi link awkRepeat Repeat
hi link awkStatement Statement
...
hi link awkNumber Number
hi link awkFloat Float
...
hi link awkComment Comment
...
endif
let b:current_syntax = "awk"
The file above uses syntax keyword to give names
to certain classes of keywords (such as real Awk keywords and built-in
functions), and syntax match
to give names to regular expressions that match certain kinds of
objects (such as numbers).
Then the hi link statements link the named classes
of objects to the predefined highlighting conventions.
The syntax.vim file predefines the standard
conventions, with a number of lines like these:
hi Comment term=bold ctermfg=Cyan guifg=#80a0ff
hi Constant term=underline ctermfg=Magenta guifg=#ffa0a0
hi Special term=bold ctermfg=LightRed guifg=Orange
hi Identifier term=underline ctermfg=DarkCyan guifg=#40ffff
...
The first argument defines the class, and the rest define what kind
of highlighting to do on what kind of terminal. term
is for a normal terminal, cterm is for a color
terminal (in this case, the ForeGround color), and gui
is for vim's GUI interface.
In vim, the syntax colors are global
attributes. Changing the Comment color
changes the color for all comments in all windows, no
matter what programming language you're editing.
Since the syntax descriptions use attribute linking, you can
make language-specific changes. For example, to change the comment
color for Awk, you can define attributes for awkComment,
like this:
hi awkComment guifg=Green
vim comes with a large number of syntax descriptions
for different languages. The coloring for Awk is
slightly psychedelic (lots of red and pink), although the coloring
for context diffs is actually rather pleasant,
as is the color scheme for UNIX mailbox files.
The HTML mode is also pretty interesting.
Overall, it's quite a lot of fun to use.