September 7th, 2005
Short Reference Guide to vi Commands
Short Reference Guide to vi Commands
Moving Around
h, j, k, l – moves one space or line left, down, up, right.
^D, ^F, ^U, ^B – moves 1/2 or a full screen at a time.
0 – moves to the beginning of a line
$ – moves to the end of a line.
H, M, L – moves to top, middle or bottom of screen
G – moves to end of file
1G – moves to first line of file.
nG – moves to line n in file.
w – moves forward a word
b – moves backward a word
e – moves to end of current word
‘ (single-quote) – moves back to “previous” location.
(, ) – move to the beginning (end) of the current sentence.
{ , } – move to the beginning (end) of the current paragraph.
Getting rid of Things
d
dd – a line
dw – a word
d$ = D – from here to end of current line
d0 – from here to beginning of current line
x – one character
dG – delete from here to end of file.
Adding and Changing Text
i – enter insert mode to left of current cursor position
I – insert at beginning of current line
a – enter insert mode to right of current cursor position
A – insert at end of current line
r – replace a single character
R – enter overstrike mode.
c
u – undo the last text addition or deletion
:i,js/
y
Y – yank an entire line (or lines)
Saving Changes, Quitting, reading and writing files
ZZ – save the current file and exit.
:w – write using current file name
:w
:w! – write, even though a file exists with that name.
:q – quit without saving
:q! – really quit.
:wq – write and then quit
– exit while saving changes
:r
Marking lines and Finding Patterns
mx – mark the current position, and name it x (legal names are a-z, inclusive)
/
?
n – find the next instance of the pattern.
N – find the next instance of the pattern backwards.
Miscellaneous Commands
^Z – suspend execution of vi and return to the shell (see the jobs and fg commands to see how to return to vi)
^V
~ (tilde) – change the case of the current character.
n>> – move indentation n lines right shiftwidth spaces
n< < - move indentation n lines left shiftwidth spaces
>
<
. (period) – repeat the last insert or deletion command.
Hints and Tips
xp – swap two letters ddp – swap two lines
dwwp – swap two words dwbp – swap two words backwards.
There are 26 named buffers defined in vi; these buffers can be used to hold text in cut and paste operations and to hold boilerplate for memos, letters, etc.
“xy
Example: “ay9
“xp – will put the contents of the buffer “x at the current cursor position.