Parliament Hill Computers LtdParliament Hill Computers Ltd

Vi/Vim quick reference

Vi/Vim differences and quick reference

The key to effective vi use is to spot the symmetries in the commands.
Note that if you have a command c then shift c will often do the same as command c but do it backwards/before, eg: `/' searches forwards, `?' searches backwards; `p' pastes text after the cursor, `P' pastes text before the cursor. Many commands can be prefixed with a count, eg: `5w' moves forwards 5 words; `5x' deletes 5 characters.

For effective use of vi you must master the movement commands, they are used in conjunction with others, eg: `dw' deletes the next word (and `5dw' or `d5w' deletes the next 5 words); `c/fred' changes from the current position to the next occurrence of `fred'.

Movement: Commands

hmove one character left
jmove one line down
kmove one line up
lmove one character right
Hmove to Home (start of first line displayed)
Mmove to start of Middle line displayed
Lmove to start of Last line displayed
0move to start of current line
^move to first non white space character on current line
$move to end of current line
tcmove forwards to just before character `c'
Tcmove backwards to just after character `c'
fcmove forwards on to character `c'
Fcmove backwards on to character `c'
;repeat last t, T, f, F command
wmove forwards to the start of the next word
emove forwards to the end of the next word
bmove backwards a word
%move to matching bracket: () [] {}

The arrow, home and end keys should also work as expected.

Movement: Searching

/Search forwards for regular expression
?Search backwards for regular expression
nrepeat last search in the same direction
Nrepeat last search in the reverse direction

Inserting text

iInsert before the current character
aInsert after the current character
oOpen a new line after the current line
OOpen a new line before the current line
IInsert before first non white space character on the current line
AInsert after the last non white character on the current line

Entering insert mode: In insert mode characters are inserted into the buffer, to leave it type Escape. To insert a special character type `C-vc' which inserts character `c' without interpretation (useful with Escape).

Deleting text

xDelete the character under the cursor
dMDelete to where the movement `M' would take the cursor
ddDelete the current line
DDelete from the current position to the end of line

Deleted text is put into the current yank buffer, you may specify a buffer, eg: `"bdT:' deletes backwards to the `:' and puts the deleted text into yank buffer `b'. See Yank buffers below for multiple delete undo via numbered yank buffers.

Changing text

rcReplace the current character with `c'.
sSubstitute the current character: enter insert mode
cMChange to where the movement `M' would take the cursor: enter insert mode
ccChange the entire current line: enter insert mode
CChange from the current position to the end of line
RReplace text, end with Escape
oopen new line below, end with Escape
Oopen new line above, end with Escape

Yank buffers

These store text, if a buffer is not named text is put into the current yank buffer. Deleted text in put into buffer '1', text that was in `1' is put into `2', ... to `9'. Buffers can also be named with a lower case letter, these names buffers may be used to copy text between files. To use a named buffer prefix a command with `"N', eg: `"add' deletes the current line and puts it into yank buffer `a', `"ap' pastes the contents of yank buffer `a'.

yMCopy text into yank buffer to where the movement `M' would take the cursor
yyCopy the current line into the yank buffer
YCopy from the current position to the end of line into the yank buffer
pPaste the text into the yank buffer after the current line
PPaste the text into the yank buffer before the current line

Colon commands, these are based upon ed(1) commands

In many of these `%' will be substituted with the name of the current file, and `#' with the name of the previous file. Some commands may be prefixed with line number ranges. `.' means the current line, `$' means the last line in the file: `2,5 s/fred/joe/' changes the first `fred' for `joe' on lines 2, 3, 4, 5; `1,$ s/fred/joe/g' changes every `fred' for `joe' in the buffer. `%' may be used as a number range and is short for `1,$'. Number ranges may also be: `.+N' (N lines after current line), `$-N' (N lines before end of buffer), `/fred/+N' (N lines after next `fred'). If you only want one line, omit the comma and second number: `.-4 s/fred/joe/'.

:q!Quit without the sanity checks (unsaved buffers, etc)
:w [f]Write the buffer to file `f'
:w !cmdWrite the buffer as stdin to shell command `cmd'
:wqWrite and quit
:xLike `:wq' but only write if changes have been made
:r fileRead file into the current buffer after current line
:e fEdit file `f'.
:e #Edit the previous file, often bound to C-^
:nEdit next file
:pEdit previous file
:rewRewind to first file
:! cmdExecute shell command `cmd'
:s/a/b/Substitute `a' with `b' on the current line.
:s/a/b/gSubstitute every `a' with `b' on the current line. (g means global)
:s/a/b/gcSubstitute every `a' with `b' on the current line, ask for confirmation
:dDelete the current line

Display (screen) control

C-escroll line with cursor up a line
C-yscroll line with cursor down a line
C-dscroll display down 1/2 a screen
C-uscroll display up 1/2 a screen
C-fscroll display forward a screen
C-bscroll display backward a screen
C-lredisplay the screen


JJoin the next line to the current line
uundo the last change, vim: repeat for previous changes vi: a second `u' undoes the first undo
C-rRedo a change undone by `u' (vim only)
UUndo all the changes made to the current line
.Repeat the last change command
~Invert case (upper/lower) of the current character
!!cmdReplace the current line with the current line filtered by the shell command `cmd'
C-gDisplay current line number
GMove to the last line in the buffer
nGMove to line number n in the buffer
mlMark the current line with the label `l'
'lGo to the line with label `l'
''Go to the last line that you jumped from Labels may be used in `:' commands: `'s,'e:s/fred/joe/g' changes all `fred' for `joe' for all lines between those labeled `s' and `e'.
nCRepeats the command C n times
qrStart recording into register `r' (vim only). If `r' in upper case append
qStop recording
@rExecute keystrokes in register `r'
<<Move text left by an indent
>>Move text right by an indent
C-zSuspend (shell job control)

Visual (block) mode (vim only)

vStart character visual mode
VStart line visual mode
c-VStart block visual mode
Once a visual area is marked:
~swap case
umake lower case
Umake upper case
!filter through external program
:disDisplay the contents of all yank buffers

Split windows and multiple buffers (vim only)

C-wsSplit the window in two
C-wcClose the current window
C-wMMove to window in direction M, eg `k' means up
NC-w+Increase display size of current window by N lines
NC-w-Decrease display size of current window by N lines
:filesDisplay files currently being edited and buffer numbers
:N bSwitch to buffer N in the current window

Useful options (put in ~/.exrc)

set ic Ignore case in searches
set numberLine number mode
set showmodeDisplay `INSERT' on bottom line in insert mode
syntax offSwitch off syntax highlighting

Misc vim additions (ie not in vi)

Handy sequences

If you want a tutorial use the command:


The commands below are the most commonly used commands, but by no means all.

Other resources on vi/vim

Return to tutorial home.

If you want any help using the above, or have any comments or suggestions, please contact us.