vim has extensive facilities for both the edit-compile-debug cycle and syntax highlighting.
The facilities in vim were inspired by the "quick fix" mode of the Manx Aztec C compiler for the Amiga. In fact, the vim documentation refers to this feature as "quick fix" mode. The features are quite flexible, allowing you to tailor them to your programming environment (see Table 11.12).
Like elvis, as you move through the errors vim also compensates for changes in the file, so that when you go to the next error, you end up on the correct line.
The vim options that control the :make command are presented in Table 11.13.
When you execute :make, vim constructs a command by concatenating the various pieces described above. Any arguments you supply are passed to make in the appropriate place. It then echoes this command to your screen. For example, if you type :make -k, you might see something like this:
:!make -k 2>&1| tee /tmp/vim34215.err ...
By using the tee(1) program, the output from make and the compiler is saved in the error file (/tmp/vim34215.err), and also sent to standard output, in this case your screen.
When the make finishes, vim reads the error file, and goes to the location of the first error. It uses the value of the errorformat option to parse the contents of the error file, in order to find file names and line numbers. (The format of this variable is described in full in :help errorformat.) You can then use the :cc command to see the error messages, and the :cnext command to move to the next error.
Highlighting in vim is based primarily on colors. To enable syntax highlighting, put syntax on into your .vimrc file. This will cause vim to read the syntax.vim file, which defines the default highlight coloring and then sets things up to use highlighting appropriate to each language.
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 <[email protected]>
" 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.
Copyright © 2003 O'Reilly & Associates. All rights reserved.