An Introduction to VIM

VIM is a powerful text editor.

This document has been written to help people who can already use vi to learn some of VIM's features.

 

Word Completion

CTRL-n
Next match.
CTRL-p
Previous match.

To enter:

for (flightplanIt = fps.begin(); flightplanIt != fps.end(); ++flightplanIt)

Type:

for (flightplanIt = fps.begin(); fl<CTRL-p> != fps.end(); ++ f<CTRL-p><CTRL-p>)

The VIM complete variable (set using ":set complete=options") specifies where VIM will search for words to use when doing word completions (e.g. adding `]' to the option list will cause VIM to search your tags file).

 

Running grep from within VIM

VIM has a nice interface to the grep command. (grep can be used to find where a search pattern appears in a collection of files.)

:grep options search_pattern files_to_search
VIM will automatically take you to the first match.
:cn
See ("c") next match.
:cp or :cN
See previous match.
:cw
(This command was added in version 6.0.)

Display a window which lists all matches.

Double-click on an entry and VIM will load the corresponding file and take you to the selected match.

 

Undo

u
Undo last change. You can undo multiple changes by pressing `u' several times.
CTRL-r
"Re-do" (undo last undo).
 

Visual selections

Visual selections are an easy way to specify the text that a command will operate on. Once a selection has been made, pressing d will delete the selection, y will copy ("yank"), > will increase indentation, etc.

v
Start a visual selection. Move cursor to specify end of selection.
V
Enter visual mode: Whole lines will be selected.
CTRL-v
Enter visual mode: A column will be selected.
 

Folding

A fold displays several lines as a single line. You can fold "uninteresting" lines so that you can focus on the lines which are relevant to your current task.

Folding was added in VIM version 6.0.

zf
Create a fold.

To select the lines to be folded, either make a "visual selection" or follow zf with a "movement" command.

za
Open/close an existing fold.
 

Moving around

CTRL-o
Return to old location.

VIM remembers each jump you perform: If you want to return to a place you recently visited then you can press CTRL-o several times until you return there.

CTRL-i
Return to a newer location (undoes effect of CTRL-o).
zt, zz, zb
Scroll window so that current line is at the top, middle or bottom of window.

I like to use zt after I do a tag jump to a function definition.

 

Searching for things in current buffer

/pattern
Search forward for first match of pattern.

To search backwards type ?(Shift-/) instead of /. (Note: The Shift key is often used in vi to perform an action in reverse.)

:set hlsearch
All matches will be highlighted (shown in a different color).
:set nohls
Matches will no longer be highlighted.
:nohls
Matches for the current search pattern will no longer be highlighted. However, highlighting will automatically be re-enabled for the next search.

I have Shift-<ESC> mapped to do ":nohls". (I added

nmap CTRL-v Shift-ESC :nohls CTRL-v ENTER

to my .vimrc file.)

#
Find previous match for word under cursor.

(This is much quicker than typing ?\<word_under_cursor\>.)

*
Find next match for word under cursor.
 

Using tags

Tags associate a symbol to a location in a file. Tags are often used by programmers to quickly find function definitions.

A tag file for C source code can be generated with ctags.

(VIM has a nice interface to cscope. cscope can be used to find symbol definitions (as with regular tags), but can also quickly perform other searches such as finding everywhere where a specified function is called.)

CTRL-]
Go to first tag found which matches the current word.
g CTRL-]
Jump to tag (uses current word). If there are several matches you'll be asked to choose from a list.
:tj tag
Jump to tag.
:ptj tag
Jump to tag in a "preview" window. (Current buffer will remain visible.)

:ptj could be useful if you want to see the types of the parameters which should be passed to a subprogram.

CTRL-t
Return from tag jump.
:tags
List the tag stack (shows how you got to where you are).
:set tags=tag_file_list
Set the tag files to use. By default VIM will use a tag file in the current directory.
 

Command line editing

VIM keeps a list of the commands you have entered. Use the up and down arrow keys to move through the list of commands.

Typing the start of a command and then pressing the up key will cause VIM to find the last command which started with the text you just entered (e.g. :tj <Up> will retrieve the last tag jump command you entered).

q:
Open command editing window.

You can use normal VIM commands to edit command lines (e.g. `$' will take you to end of line).

Don't forget to type `i' (insert) before you enter the text for a new command.

 

Syntax highlighting

VIM can show pieces of text in different colors. For example; program comments can be shown in a particular color so that they can be immediately recognised.

VIM can color ()'s in Java source code a different color depending on the level of nesting! This helps in identifying a matching bracket. Type :help java-syntax for more information. (Note: in VIM 6.1-247 it was necessary to modify the syntax/java.vim file to get this feature to work properly. I had to add "transparent" to the definitions for javaParenT, javaParenT1 and javaParenT2.)

 

Specifying text to modify

Some commands allow using a "movement" as a way to specify the text which the command should affect. Updates can be made very quickly this way.

d%
% normally moves to a matching bracket or brace. When I want to delete a block of code in a C program I use "d%" (it's a lot quicker than making a selection with the mouse and then pressing <Del>).
y2f)
y specifies that a copy ("yank") is to be performed. 2f) would normally find the 2nd `)' to the right of the cursor on the current line. Put together, this command copies everything from the current position up to and including the 2nd `)' on the current line.
 

To find out more

:help
VIM has excellent on-line help.

(You can use tag commands to move between topics.)


Last Update: 6 January 2006