VI Editor Quick Reference

      The vi text editor is a mode oriented editor. In a mode oriented editor, the manner in which keystrokes are interpreted depends on the current mode of the editor. In vi, the two main modes are the command mode, from which anything typed is interpreted as a command, and the input mode, in which keystrokes are seen as simple text. Note that vi commands are case sensitive.

To invoke vi type:

vi filename

      vi begins in the command mode. The input mode can be started with any of the "entering text" commands below. To return to the command mode, hit the key.

      Below is a list of essential vi commands. Note commands with a # before it denote to repeat the command a number of times or to move to the line of that number:

Scrolling
<ctrl>d      scroll down a half screen
<ctrl>u      scroll up a half screen
<ctrl>f       scroll forward a screen
<ctrl>b      scroll backward a screen

Cursor
positioning

#j                   down #number of lines(s)
#k                  up #number of lines(s)
#h                  left #number of space(s)
#l                   right #number of space(s)
arrow keys      replace h,j,k, and l commands
#G                 line #of the file
G                   last line of the file
H                   top of the screen
M                   middle of the screen
L                    last line of the screen
O (zero)          beginning of current line
$                    end of current line

Entering
text

a       append text after cursor
i        insert text before cursor
o       open a new line below the current line
O      open a new line above the current line

Changing
text

#x         delete # number of character(s) after and including cursor
#dd       delete # number of line(s) including current line
D          delete remainder of line to the right of cursor
J           join next line to the current line
r            replace character with the next typed character

Copying
changes

#yy or #Y       yank # number of line(s), including current line to buffer
p                   put line(s) from buffer below the current line

Undoing
Changes

u       undoes last command
U       undoes all changes made to current line before leaving line

Searching
and
replacing

/pattern        search for next occurence of pattern
?pattern       search for preceeding occurence of pattern
n                  repeat the last search in the same direction
n                  repeat the last search in the opposite direction

:n,ks/oldpattern/.newpattern/g            search for oldpattern and replace                                                            with newpattern in lines n thru k

:%s/oldpattern/.newpattern/g              global search and replace

Saving
text and
exiting vi

:w                  write to disk and remain in vi
:q                  quit, ignoring changes since last :w
:q!                 emphatic form of quit. Use when :q fails
:wq                write to disk and quit vi
ZZ                  same as :wq
:n,kw filename                write lines n thru k into filename
:n,kw >> filename           append lines n thru k to filename

For more information on vi one can look at:

Back to the table of contents