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.
Insertion and Deletion top of page
i | puts you in insert mode, beginning at left of the current character |
I | inserts at the beginning of the line |
a | puts you in insert mode, beginning at right of the current character |
A | puts you in insert mode, starting at the right of the last character on current line |
o | open a new line below current line and enter insert mode |
O | open a new line above current line and enter insert mode |
x | delete current character |
X | 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 |
j | down, one line |
k | up, one line |
h | left, one character |
l | right, one character |
0 | start of line |
$ | end of line |
H | home - first character of first line on current page |
M | first character of middle line on current page |
L | first character of last line on current page |
w | next word |
b | previous word |
W | next word delimited by white space |
B | previous word delimited by white space |
: 1
1G |
start of file |
G | 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 |
p
|
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 ... |
R | overwrite mode |
s | change mode - like insert except that current character is deleted |
S | input mode - like insert except that current line is deleted |
File Commands top of page
ZZ | save and quit |
: w | save file |
: w! <newfilename> | save file as <newfilename> |
: e <filename> | edit file <filename> - creates new file if it doesnt exist |
: q | 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 |
J | 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.