Parliament Hill Computers LtdParliament Hill Computers Ltd

Vim quick reference


Vim quick reference

This is a quick Vim reference, it takes you through the things that you should know. It is not a complete reference.

The older (Unix) Vi editor is much the same but does not do all that vim does. Only some differences are noted here.

The key to effective vim use is to spot the symmetries that often exist in the commands.

For effective use of vim 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

These move the position of the cursor in the buffer

hmove one character left
jmove one line down
kmove one line up
lmove one character right
C-nmove one line down
C-pmove one line up
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 forwards to sentence start
(move backwards to sentence start
}move forwards to paragraph start
{move backwards to paragraph start
C-gDisplay the current line number
ngmove to line number n
Gmove to last line
%move to matching bracket: () [] {}

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

C-x’ means press the Control key when you press ‘x

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

When ‘/’ or ‘?’ is pressed the cursor moves to the bottom line of the screen which is where you type the search string (regular expression)

Marking lines

You may give 26 positions names, named with a letter (a .. z).

mamark current position with the name a
`areturn the position a
'areturn the first non white space position of the line marked a
''return to the last line that you jumped from
:marksshow current marks

Note the difference between the look-similar characters: ‘`’ (grave accent) & ‘'’ (single quote)

There are another 26 positions that are named with a CAPITAL letter (A .. Z). Whereas the lower case letters (a .. z) are marks within a buffer, the marks A .. Z remember the file name as well, so returning to one of them will reopen the file.

Display (screen) control

These commands change which lines in the buffer are displayed.

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

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

dM’ deletes from the current position to where the movement command ‘M’ would take the cursor. Illustrations:

Deleted text is put into the 'unnamed' yank register, you may specify a register, eg: ‘"bdT:’ deletes backwards to the ‘:’ and puts the deleted text into yank register ‘b’. See Yank registers below for multiple delete undo via numbered yank registers.

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

Again: the ‘M’ of ‘cM’ is a movement. Eg:

Yank registers

These store text that can be pasted into an edit buffer. Yank registers can be used to copy text between edit buffers.

yMCopy text into yank register to where the movement ‘M’ would take the cursor
yyCopy the current line into the yank register
YCopy from the current position to the end of line into the yank register
pPaste the text in the yank register after the current line in the edit buffer
PPaste the text in the yank register before the current line in the edit buffer
:disDisplay the contents of all yank registers

Again: the ‘M’ of ‘yM’ is a movement. Eg:

Named yank registers

There are 26 yank registers, named with a letter (a .. z). The above commands may be prefixed by this name: ‘"a’ operates on yank register ‘a’, eg:

There are another nine yank registers, named with a digit (1 .. 9). Deleted text in put into register ‘1’, text that was in ‘1’ is put into ‘2’, ... to ‘9’. An unnamed yank register is used if a yank command is not prefixed by a name.

Many other commands can be prefixed by a register name, eg:

The contents of the yank registers may be shown with the :registers command. Abbreviate to :reg or :dis

Beware: there is a maximum size of what a register can contain

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

When you enter a ‘:’ the cursor is moved to the message line (at the bottom of the screen) where you enter the rest of the command. You can recall previous commands with up-arrow and then edit them.

:qQuit
:q!Quit without the sanity checks (unsaved buffers, etc)
:wWrite the buffer to the file that it was read from
:w fWrite the buffer to file ‘f
:w! [f]Write forcefully, eg if permissions/ownership would not allow
:w !cmdWrite the buffer as stdin to shell command ‘cmd
:wqWrite and quit
:xLike ‘:wq’ but only write if changes have been made
ZZSame as ‘:x
:r fileRead file into the current buffer after current line
:e fEdit file ‘f’.
:e! fEdit file ‘f’, do not warn if current buffer has not been saved
:nEdit next file
:pEdit previous file
:rewRewind to first file
:! cmdExecute shell command ‘cmd’. If a range is given the lines are filtered
: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
:d nDelete the n lines, several commands take a number
:m nMove lines to position n
:pprint — show the lines
:las print but show line end & unprintable characters
:helpHelp. May be followed by a help topic

In many of these ‘%’ will be substituted with the name of the current file, and ‘#’ with the name of the previous file. Eg:

Colon command number ranges

Some of the colon commands can act on many lines. These can be given zero, one or two address ranges:

Line numbers can be specified in several ways:

Examples:

Misc

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
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)

Examples:

Visual (block) mode (vim only)

Start visual mode, move the cursor somewhere else (the area back to the start point is highlightened), enter a command that will be applied to every highlightened character. vStart character visual mode VStart line visual mode c-VStart block visual mode

Example: once a visual area is marked, the text in the region:

ddelete
cchange
yyank
~swap case
umake lower case
Umake upper case
!filter through external program

Split windows and multiple buffers (vim only)

The screen may be split into multiple windows and different buffers (files) edited in the different windows. The normal yank/paste commands enable copying between buffers.

C-wsSplit the window horizontally in two (top/bottom)
C-wvSplit the window vertically in two (left/right)
C-wcClose the current window
C-woMake the current window the only one on the screen
C-wMMove to window in direction M, eg: ‘k’ means up
NC-w+Increase height of current window by N lines
NC-w-Decrease height of current window by N lines
NC-w>Increase width of current window by N columns
NC-w<Decrease width of current window by N columns
: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:

vimtutorial

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

Other resources on vi/vim

http://www.viemu.com/a-why-vi-vim.html
http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
http://wiki.init.cc/_media/docs/tools/vim_tips1.pdf

Return to tutorial home.

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