vi - Quick Reference

This material is intended to serve as a very brief and quick reference to vi. It is certainly not comprehensive. No claim is made for its accuracy. In case you find any discrepancies, please bring them to my notice via email and I shall try to incorporate the changes.


vi has 2 modes - one is the normal mode and the other the insert mode. This may take some getting used to in the beginning as it is a bit unusual. One can get into the normal mode by hitting the Esc key. Once in normal mode, vi interprets the user input as commands instead of putting what you are typing into a file.
Following are some of the commands one may need frequently. In the following, current line or character refers to the one on which the cursor is placed. Commands starting with a : should be ended with <return>

Insertion and Deletion      top of page
puts you in insert mode, beginning  at left of the current character
inserts at the beginning of the line
puts you in insert mode, beginning at right of the current character
puts you in insert mode, starting at the right of the last character on current line
open a new line below current line and enter insert mode
open a new line above current line and enter insert mode
delete current character
delete character left of cursor
dd  delete current line. This can also be used for cut and paste (with p).
d<return>  delete the current and next line.
dw  delete to end of current word
d$  delete to end of line
d0  delete to start of line starting from character left of cursor

Moving around      top of page
Ctrl-f  forward, one page
Ctrl-b  back, one page
Ctrl-u  up, half a page
Ctrl-d  down, half a page
down, one line
up, one line
left, one character
right, one character
start of line
end of line
home - first character of first line on current page
first character of middle line on current page
first character of last line on current page
next word
previous word
next word delimited by white space
previous word delimited by white space
: 1
1G
start of file
end of file
: n
nG
goto n-th line of the file

Cut and Paste      top of page
yy  yank / copy current line to buffer

 
paste contents of buffer after current character / line. Note that each entity deleted or yanked is placed into the buffer. Also its better to paste the buffer contents as soon as you have yanked or cut them they may be lost while you do other things. 

Search and Replace      top of page
/<string>
 
search forward, starting from current character, for the first occurence of <string>. use 'n' to get to each next occurance. The search may or may not wrap after the end of the file depending on your option settings.
?<string>
 
search backward, starting from current character, for the first occurance of <string>. use 'n' to get to each next occurence. The search may or may not wrap after the start of file depending on your option settings.
: s/<oldstring>/<newstring>[/gc]  substitute the first occurance of <oldstring> in current line with <newstring>. If /g option is specified then all occurences are replaced. If /c option is specified, either with or without /g, then a confirmation is asked before each occurence is replaced.
r<char> replace current character with <char>
cw <newstring> replace characters to the end of current word with <newstring> . When you use c, vi displays a $ sign at the last character that will be replaced. Guess what c$ and c0 do ...
overwrite mode
change mode - like insert except that current character is deleted
input mode - like insert except that current line is deleted

File Commands      top of page
ZZ  save and quit
: save file
: w! <newfilename> save file as <newfilename>
: e <filename> edit file <filename> - creates new file if it doesnt exist
: quit file - won't let you if file has any unsaved modifications
: q!  quit file regardless of any unsaved modifications, if any
: r <filename> read file <filename> after current line

Miscellaneous Commands      top of page
<Esc> switch to normal mode
join the current and next lines
Ctrl-l  redraw screen
Ctrl-g  show info about current line, file and the percentage of lines in the file that are above the current line.
! command execute command as shell and return

 

Note: n<cmd> can be used to perform the <cmd> n times where <cmd> can be any one of dd, dw, dW, h, j, k, l, r, w, W, x, X or yy (also some others). This can be pretty useful at times. Try out cnw replacing n with a number.