vi Quick Reference


Starting vi

To create a new file, or edit an existing file, type vi filename at the system prompt.

Moving Around in the File

Ctrl-b    Moves backward a page.
Ctrl-d Scrolls down half a page.
Ctrl-f Moves forward a page.
Ctrl-l Refresh the screen.
Ctrl-u Scrolls up half a page.
b Moves the cursor back one word.
e Advances the cursor to the end of the current word.
G Moves the cursor to the last line.
nG Moves the cursor to line number n.
h Moves the cursor one space left.
j Moves the cursor one line down.
k Moves the cursor one line up.
l Moves the cursor one space right.
0 Moves the cursor to the start of the current line.
w Advances the cursor to the next word.
) Moves forward to the next sentence.
( Moves backward to the previous sentence.
^ Moves the cursor to the beginning of the current line.
$ Moves the cursor to the end of the current line.
% Finds matching brace or parenthesis.

Note: Most vi movement and editing commands can be automatically repeated by typing the number of repeats before the command (e.g. 3j moves the curson down 3 lines).

Text Input Commands

a    Appends (just like i except starts one character later).
A Appends to end of line.
i Inserts before cursor.
I Inserts at beginning of line.
o Opens (makes an empty line after the current one, and inserts).
O Opens above (like 'o' except it makes an empty line before the current one).

Note: To leave the 'i', 'a', or 'o' modes to do other commands, press the <ESC> key. For keyboards with no separate <ESC> key, use Ctrl-[, which sends the same ASCII code.

Editing Commands

cc Changes the line until <ESC> is pressed.
cw Changes the word until <ESC> is pressed. The end of the word is flagged by $. More or less text than the original can be inserted.
dd Deletes an entire line.
dw Deletes rest of word from cursor position.
d) Deletes rest of sentence.
d} Deletes rest of paragraph.
D Deletes to end of the line. Also use 'd$'.
J Join next line to the line the cursor is on.
r Replaces the character the cursor is on.
R Writes over old text.
s Substitute character with text and keep inserting until <ESC> is pressed.
x Deletes a character.
~ Changes a character's case.
:r file Reads in a file where the cursor is positioned.
:r !command Reads in the results of command (date, cal, finger, etc.)
:s/string1/string2/ Substitute string1 by string2 in current line (1st occurrence on line).
:s/string1/string2/g Substitute string1 by string2 in current line (all occurrences on lin e).
:n,Ns/string1/string2/ Substitute 1st occurrence of string1 by string2 in lines n through N.
:%s/string1/string2/g   Substitute all occurrences of string1 with string2.

Cut/Copy and Paste Commands

p Puts last text yanked or deleted after the cursor.
P Puts last text yanked or deleted before the cursor.
yw Yank a copy of the word from cursor to end of word.
yy Yank a copy of a line.
y) Yank to the end of the sentence.
y} Yank to the end of the paragraph.
y$ Yank to the end of the line.
y3w    Yank the next 3 words.

Search Commands

n Continues searching in the same direction. Use with the :s or / command.
N Searches in the opposite direction.
?word Searches for word backwards in the text before the cursor.
/word    Searches for word forward in the text after the cursor.

Three Special Commands

u    Undoes the last command you used.
U Undoes all changes made to the current line.
. Repeats the last editing or insert command.

Write & Exit Commands

:q Quits and leaves the editor.
:q! Forces exit without saving changes.
:w Writes the file and saves the changes.
:w (name) Writes to the file (name).
:wq Writes and quits at the same time.
:.,$w (name)    Writes rest of current file to (name).
:x Same as :wq.
ZZ Same as :wq.

©2010 NSCEE