logo

Quick Referemce for the Vi Text Editor

The most common editor for a UNIX machine like bgunix is the vi editor. To use vi to create or edit a file, use this command:

vi filename

There are two different operating modes:

  1. Command mode This is the normal and initial startup state. You can enter commands but no text.
  2. Insert mode In Insert mode you can type text into the file, but you cannot enter any commands. To enter a command you must first return to Command mode by pressing the ESC key.

Commands to change from Command mode to Text mode

i inserts text at cursor position
I inserts text at the beginning of the current line
a appends text after the current character
A appends text at the end of the current line
o adds a blank line below the current line
O (letter O) adds a blank line above the current line

Commands to save the file and/or quit vi

ZZ writes the file to disk and quits
:wq writes the file to disk and quits
:q quits the session if nothing changed in document
:q! quits the session without saving any changes to the file
:w filename writes the buffer contents to a new filename without exiting to shell; will not write to an existing file
:w! filename writes the buffer contents to an existing filename, overwrites contents of existing file

Commands to move around in your document

cursor (arrow) keys moves up, down, left, or right one position
$ moves to end of line
0 (zero) moves to beginning of line
H moves to the top of the screen
M moves to the middle of the screen
L moves to the bottom of the screen
G moves to the last line of the file
nG moves to line n of the file (e.g. 7G moves to line seven)
Ctrl-F moves to the next page (hold down Ctrl, press F)
Ctrl-B moves to the previous page (hold down Ctrl, press B)

Commands to delete or replace parts of the document

x deletes the character under the cursor
X deletes the character before the cursor
D deletes from the character under the cursor to the end of the line
dd deletes the current line. (The line deleted is put into the General-purpose Buffer)
r replaces the character under the cursor and stays in Command mode
R replaces the character under the cursor and remains in overwrite mode (following text will be overwritten)

The x, X, and dd commands can be preceded by an integer to delete multiple characters or lines (e.g. 12x or 12dd).

Commands to move lines within the document

dd deletes the current line, putting it into the General-Purpose Buffer
ndd deletes n lines starting with the current line, putting them into the General-Purpose Buffer
yy copies (yanks) the current line to the General-Purpose Buffer
nyy copies (yanks) n lines starting with the current line, putting them into the General Purpose Buffer
p inserts the lines from the General-Purpose Buffer just after the current line
P inserts the lines from the General-Purpose Buffer just before the current line

Commands to search and replace a string

/searchstring searches forward for searchstring
?searchstring searches backward for searchstring
n repeats the last search
N repeats the last search in the opposite direction
:%s/searchstring/replacestring replaces the first searchstring occurrence with replacestring
:%s/searchstring/replacestring/g replaces all searchstring occurrences with replacestring

Other commands

:set number displays line numbers
u will undo the last instruction