Firstly I'd like to thank everyone who has mailed me words of appreciation for
the two preceding tutorials on ViM that I
wrote and also those who have mailed me to ask for more.
Which version of ViM
Some of the features that I am going to describe in this tutorial are onlu
available in version 5 of ViM - and even then only if those features were
enabled at compile time.
To find out what version you are using, and what the active feature set is, use
the :version command. By using this command, vim will display its
version number and list the set of features that are available with it.
Features named with a '+' in front were enabled at compile time, those with a
'-' were not.
The vimrc file
I'm going to explain a bit about the 'vimrc' file. If you already know about
it you can move on to the next section.
The vimrc file is a plain text file which is looked at when you start ViM.
It is hidden under Linux, in that it is called '.vimrc', and kept in your
home directory. The file is used to store commands that you would otherwise
have to type in each time that you start up the editor. You can only have one
command per line, and these commands can be commented out with a double quote
For example, most vimrc files will have 'set wrapmargin=1' in them somewhere so
that wordwrap is turned on automatically. With the abbreviation command that
I covered in the last
tutorial you can also use the vimrc file so that ViM will correct your
more typical spelling mistakes.
Over time, your vimrc file could grow to be quite big. It could also get a bit
complicated to keep it all in synch - especially if you use vim on more than
one machine and a portion of one vimrc is machine specific and you don't
want to use an exact copy on the other machine.
There is a nice solution to this scenario, break down the vimrc file into
smaller files and use the 'source' command in your vimrc file.
The source command takes the form of 'source '. What it does is
read that file and execute the commands that are in it.
Though unlikely, you could end up with a small vimrc file such as:
There's no real requirement to use the '.vim' extension, it will come in
handy though if you want them colour-coded when they are edited with syntax
highlighting turned on.
On another slightly related topic, you might want to move some of your vim
files to somewhere such as /usr/local/vim so they can be used from within all
accounts on your machine.
Syntax Highlighting (Colour Coding by a different name).
A very nice thing about version 5 of ViM is that you can tell it to colour
code or 'syntax highlight' the text that you are editing. And there's really
nothing to it.
If you want to use this to colour code for HTML syntax simply add the folowing
line (though you may need to change it slightly) to your vimrc file.
I found out where this html.vim file was through the 'locate' shell
command. The corresponding file needed for syntax highlighting C code should be
in the same directory as html.vim, and is called c.vim.
Just adding this line to your vimrc file isn't really enough, you have to
enable this by typing in the ':syntax on' command. If, for some reason,
you need to turn this syntax highlighting off then ':syntax off' will do
You should only have to do this if the global vimrc file in the /etc directory
has the line for syntax highlighting commented out. This seems to be the case
by default which really is a shame as the disabled line sources syntax.vim.
This file, syntax.vim, sets things up so that a file with any recognized
extension or full filename in the case of some files) will be colour coded by
default when you load it into ViM. Needless to say if you want to set up syntax
highlighting for yourself you really should 'source' syntax.vim rather than any
There is a whole wealth of files for syntax highlighting that come with ViM.
If you are a bit curious of what these files look like you'll find them in
Because syntax highlighting in vim revolves around recognising patterns in the
text rather than the context of the text don't be suprised if you see the
occasional word incorrectly highlighted. For example, with syntax on, the line
ab liek like
in a file with a '.vim' extension the word 'like' will be displayed in yellow,
which shouldn't be the case.
Information on syntax highlighting is available within ViM though the :help
Another way of making things easy for yourself in ViM is to set things up so
that one or two keypresses equates to typing in a longer phrase, essentially
mapping a keypress to something else.
Some key mappings are already set up in ViM when you start it, you can see what
these are by using the ':map' command.
Setting these mapping up is really not that difficult to do and can be really
worthwhile. For example, to set up macros to turn syntax highlighting on and
off again add these two lines to your vimrc file:
map <F2> <Esc>:syntax on<CR>
The first line sets up a mapping against F2 so that it equates to escaping to
command mode, typing in :syntax on and pressing Enter. Similarly the second
line sets up a mapping so that pressing F3 is the equivalent of typing the
command to turn off syntax highlighting.
map <F3> <Esc>:syntax off<CR>
The documentation for the syntax feature describes a mapping for
toggling syntax highlighting on and off:
map <F7> :if exists("syntax_on") <Bar>syntax off <Bar> else
<Bar> syntax on <Bar> endif <CR>
If you want to set up a mapping to work on only a visual selection of text then
you should use vmap (the 'v' means that the mapping works on visual
Similarly, nmap and imap apply to normal mode mappings and insert mode
For more information, type :help map.
Using ViM for editing C and C++ source files
Apart from the syntax highlighting and the ability to indent your C code
the :%!indent command (which relies on the indent utility being present
on your machine), ViM has quite a nice selection of inbuilt commands that you
can use when you next use ViM to edit your source code.
If you need to go to the other bracket in a pair of brackets, just press
Another thing you can do in ViM is type gd with the cursor in the middle
of a variable to go to the line where the variable is declared. This willonly
work with local variables, gD will do the same with global variables.
Again, there is something quite similar to this for editing header files.
Let's say for example that you're editing a .C file, to add in a reference to a
new header file that's being included with
And the next thing that you have to do is edit config.h. The quickest way to
load that config.h file into ViM is to place the cursor in the middle of the
filename and type 'gf'. Much quicker than ":e config.h', if the
file can be found in the path.
If you need to move from one end of a comment, or from one comment to the next,
then ]/ and [/ may be of interest to you. The first sequence,
namely ]/, will move the cursor to the end of the current comment, or the end
of the next comment depending on where it already is. Similarly, [/ will move
the cursor to the start of the current comment, or the start of the previous
comment, again depending on where the cursor is.
If you need to find out more about the syntax of a keyword, try K in
More information is available from within ViM, by using the :help
ViM reference card. (originally at http://www.math.fu-berlin.de/~guckes/vim/vim-3.0.refcard.html, 'brought back' by web.archive.org)
Learn more about vi
Vi filters, search & replace and more...
ViM: Variable/Word completion, indenting, macros and function navigation.
About the author, Ken Guest.