Quick Reference 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

iinserts text at cursor position
Iinserts text at the beginning of the current line
aappends text after the current character
Aappends text at the end of the current line
oadds 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

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

Commands to move around in your document

cursor (arrow) keysmoves up, down, left, or right one position
$moves to end of line
0 (zero)moves to beginning of line
Hmoves to the top of the screen
Mmoves to the middle of the screen
Lmoves to the bottom of the screen
Gmoves to the last line of the file
nGmoves to line n of the file (e.g. 7G moves to line seven)
Ctrl-Fmoves to the next page (hold down Ctrl, press F)
Ctrl-Bmoves to the previous page (hold down Ctrl, press B)

Commands to delete or replace parts of the document

xdeletes the character under the cursor
Xdeletes the character before the cursor
Ddeletes from the character under the cursor to the end of the line
dddeletes the current line. (The line deleted is put into the General-purpose Buffer)
rreplaces the character under the cursor and stays in Command mode
Rreplaces 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

dddeletes the current line, putting it into the General-Purpose Buffer
ndddeletes n lines starting with the current line, putting them into the General-Purpose Buffer
yycopies (yanks) the current line to the General-Purpose Buffer
nyycopies (yanks) n lines starting with the current line, putting them into the General Purpose Buffer
pinserts the lines from the General-Purpose Buffer just after the current line
Pinserts the lines from the General-Purpose Buffer just before the current line

Commands to search and replace a string

/searchstringsearches forward for searchstring
?searchstringsearches backward for searchstring
nrepeats the last search
Nrepeats the last search in the opposite direction
:%s/searchstring/replacestringreplaces the first searchstring occurrence with replacestring
:%s/searchstring/replacestring/greplaces all searchstring occurrences with replacestring

Other commands

:set numberdisplays line numbers
uwill undo the last instruction

Updated: 06/17/2020 01:08PM