VI Quick Reference Guide

All help refers to COMMAND mode, unless a preceding colon (:) is specified.

Note: This is only a summary of the commands available.
 
 

Invocation
vi file Edit at first line of file
vi file1 file2 Edit file1. Edit file2 via :n
vi [options] file As above, with Command Line Options
Command Line Options
+n Edit at line n
+ Edit at last line
+/pattern Edit at line containing pattern
-c Use line-oriented command mode
-e ... restricted edit mode
-l set lisp and showmatch options
-R Open file(s) in read-only mode
-wn Specify window size of n lines
-? Print a summary of all command line options
Exiting
ZZ Exit from visual mode, saving changes
:x Exit saving changes
:wq Write buffer, then quit
:w Write buffer to original file
:w file Write buffer to file
:w>> file Append to file
:n,mw file Write lines n through m to file
:e! Reedit file, discarding changes
:q Quit out of editor
:q! Quit discarding changes
Setting Options
:set all Display all option settings
:set option Enables option option
:set nooption Disables option option
:set option? Displays current value of option
:set Displays modified options
Text Input
i Insert before cursor
I Insert before first non-blank character on line
a Append after cursor
A Append at end of line
o Open and insert at line below
O Open and insert at line above
[Esc] Key ESCape key terminates input mode
Input Mode Commands
Control-@ Repeat last insertion
Control-W Erase last word entered
Control-H Erase last character entered
DELete or Backspace Same as Control-H
Control-U Erase entire line
Control-D Backtab over autoindent
0Control-D Kill all autoindent
^Control-D Kill autoindent on current line only
Control-V Next character literal (to insert control codes)
Screen Adjustment
Control-L Clear and redraw screen
Control-R Redraw only if @ (deleted) lines exist
zCR Redraw, current line at top of page
z. Redraw, current line at center of page
z- Redraw, current line at bottom of page
/pattern/z- Redraw, line containing pattern at bottom of page
zn. Redraw, set new window size to n lines
Control-E Scroll down one line
Control-Y Scroll up one line
Control-F Scroll forward one screen
Control-B Scroll backward one screen
Control-D Scroll down half screen
Control-U Scroll up half screen
Cursor Motion
H Move cursor to top of page
M Move cursor to middle of page
L Move cursor to last line of page
+ Move cursor to next line at first non-white character
- Move cursor ro previous line at first non-white character
down-arrow or j Move cursor down one line, staying in same column
up-arrow or k Move cursor up one line, staying in same column
left-arrow or h Move cursor to the left
right-arrow or l Move cursor to the right
0 Move cursor to the beginning of the line
^ Move cursor to the beginning of the line (first non-white character)
$ Move cursor to the end of the line
n| Move cursor to column n
Control-H Same as left arrow
File Motion
G Last line of file
nG Line n of file
/pattern Next line containing pattern
?pattern Previous line containing pattern
n Next / or ?
N Previous / or ?
/pattern/+n nth line after line containing pattern
?pattern?-n nth line before line containing pattern
) Next sentence
( Preceding sentence
} Next paragraph
{ Preceding paragraph
]] Next section/function
[[ Preceding section/function
`` Previous context
'' Previous context at first non-white character in line
Search Patterns
^ Beginning of line
$ End of line
. Any character
* Zero or more of previous character
[A-Z] Matches any character from A to Z
[abc] Matches a, b, or c
[^abc] Matches any character EXCEPT a, b, or c
\ Escape character for literal: \  /  $  .  ^  [  '  &  *  |  ~
\< Beginning of word
\> End of word
Marking Position
mx Mark current position with locator x
`x Return to position at locator x
'x Return to position at first non-white character in line
Yank and Put
yy or Y Yank line to buffer
yw Yank word to buffer
nyy or nY Yank n lines to buffer
p Put back lines below cursor
P Put back lines above cursor
"xy Yank line to buffer x
"xp Put from buffer x
Substitution
stext Substitutes text for character until ESCape
S Substitute for entire line until ESCape
:s/X/Y/options Substitute Y for first occurence of X
Options:  g - change every occurence in line
                  c - Confirm each change
                  p - Print each change
& Repeat last :substitute command
:g/X/s//Y/options Globally find the first X on each line and subsitute Y for X
Deleting Text
x Delete character under cursor
nx Delete n characters
X Delete character before cursor
dw Delete a word
ndw Delete n words
dd Delete entire line
dtx Delete to x in current line
D Delete to end of line
d/pattern Delete up to pattern
d?pattern Delete back to pattern
d`x Delete to marked location x
d'x Delete current line and to line which contains marked location x
d) Delete to end of sentence
d( Delete to beginning of sentence
d} Delete to end of paragraph
d{ Delete to beginning of paragraph
d]] Delete to end of secion
d[[ Delete to beginning of section
Changing Text
cw Change word
ncw Change n words
cc Change entire line
ncc Change n lines
C Change to end of current line
c/pattern Change up to pattern
c?pattern Change back to pattern
c`x Change to marked location x
c'x Change current line and to line which contains marked location x
c) Change to end of sentence
c( Change to beginning of sentence
c} Change to end of paragraph
c{ Change to beginning of paragraph
c]] Change to end of section
c[[ Change to beginning of section
rx Replace character with x
R Replace all characters until ESCape is entered
Word and Within Line Motions
w Move cursor forward by one word
W Move cursor forward by one word, including any punctuation
b Move cursor backward by one word
B Move cursor back one word, including any punctuation
e Move cursor forward to end of word
E Move cursor forward to end of word, including any punctuation
fx Search current line (only) forward for x
Fx Search current line (only) backward for x
; Repeat last f or F
, Reverse of last f or F
Undo
u Undo last change
U Restore current line
"np Retrieve from buffer n (1-9 and a-z)
"1pu.u.u.u. Scan recent deletions
Miscellaneous
. Repeat previous operation
~ Reverse case of letter and advance one position
J Join lines together
>> Shift line left one shiftwidth position
<< Shift line right one shiftwidth position
:g/pattern/p Print all occurences of pattern within the file
xp Reverse order of two letters
dwwp Reverse order of two words
ddp Reverse order of two lines

Updated 5/26/04 by Marty Froomin