VI Manual

Entering / leaving vi
Vi file Edit "file" at the top.
Vi + file Edit "file" at the end.
Vi +NN file Edit "file" at line "NN".
Vi +/PAT file Edit "file" at pattern "PAT".
Vi file1 file2 … Edit "file1", others via ":n".
Vi –r List preserved files.
Vi –r file Recover "file".
Vi –t tag Start at "tag".
View file Read-only mode.
ZZ Exit from vi, saving changes.
^Z Suspend vi for later resumption.
Vi states
Command mode Intitial and normal state.
Insert mode Entered by: a A i I o O c C s S, ESC terminate, "^C" cancel.
Last line Initiated by: ":" "/" "?" "!". ESC terminate, RETURN execute, "^C" cancel.
File manipulation
:x Exit, saving changes
:q Quit with warning.
:q! Quit, discard changes
:w Write to current file
!wq Write to current file and quit
:w file Write to "file" with warning if file exist.
:w! file Write to "file", overwriting existing one.
:w >> file Append to "file".
:e! Re-edit current file, discard changes.
:e file Edit “file”, starting at theop.
:e + file Edit file, starting at the end.
:e # Edit alternate file.
:n Edit next file in argument list.
:n file1 file2 Specify new files in argument list.
:f or ^G Show current file name and line number.
:ta TAG To tag file entry "TAG".
^] Synonum for ":ta".
Positioning within a file
G Goto last line.
NNG Goto line.
/PAT Goto next line matching "PAT".
?PAT Goto previous line matching "PAT".
n Repeat last "/" or "?".
N Repeat last "/" or "?" in reverse order.
/PAT/+NN Goto "NN"-th line after "PAT".
?PAT?-NN Goto "NN"-th line before "PAT".
]] Goto next section / function.
[[ Goto previous section / function.
% Find matching "(" ")" "{" "}".
Adjusting the screen
^F Full screen forward.
^B Full screen backward.
^D Scroll half screen down.
^U Scroll half screen up.
^E Scroll window 1 line down.
^Y Scroll window 1 line up.
^L Redraw the screen.
^R Redraw the screen, eliminate @ lines.
zRETURN Redraw, current line at window top.
z- Redraw, current line at window bottom.
z. Redraw, current line at window center.
/PAT/z- Redraw, with "PAT" at window bottom.
NNzRETURN Redraw, with line "NN" at window top.
zNN Change window size to "NN" lines.
Marking and returning
mX Mark position with letter "X".
`X Goto mark "X".
\'X Goto mark "X" at first non-white.
\`\` Previous context.
\'\' Previous context at first non-white.
Line positioning
H Goto home line in window.
L Goto last line in window.
M Goto middle line in window.
+ or RETURN Goto next line at first non-white.
- Goto previous line at first non-white.
j or down-arrow Goto next line, same column.
k or up-arrow Goto previous line, same column.
RETURN Synonym for "+".
Character positioning
^ Goto first non-white in current line
0 (zero) Goto first character in current line.
$ Goto last character in current line.
L or right-arrow or spacebar One character forward.
H or left-arrow One character backward.
^H or backspace One character backward
fX Find character "X" forward.
FX Find character "X" backward.
tX To character "X" forward.
TX To character X" backward.
; Repeat last "t" "T" "f" "F".
' Inverse of ";".
| Goto first column.
NN| Goto column "NN".
Words, sentences, paragraphs
w Goto beginning of next word.
b Goto beginning of previous word.
e Goto end of next word.
W Goto next blank delimited word.
B Goto previous blank delimited word.
E Goto end of next blank delimited word.
) Goto beginning of next sentence.
( Goto beginning of previous sentence.
} Goto beginning of next paragraph.
{ Goto beginning of previous paragraph.
Corrections during insert mode
ESC Terminates insert mode.
^C Interrupt, terminates insert mode.
^V Quotes any next (non-printing character).
^H or backspace Erase last typed character.
^W Erase last typed word.
^U Kill whole input.
\ Escapes “^H” “^W” “^U”
^D Backtab over autoindent (if set).
^D Kill autoindent for current line.
0^D Kill autoindent for the rest of the file.
Insert and replace
a Append after cursor.
A Append after end of line.
i Insert before cursor.
I Insert before first non-blank character in line.
o Open lines below current line.
O Open lines above current line.
cc Change whole line.
cO Change object "O".
C Change rest of line from cursor position.
s Substitute character at cursor with more chars.
S Substitute whole line with new text.
R Replace text overlayed as many as entered.
Miscelaneous operations
rX Replace character under cursor with "X".
x Delete character under cursor.
X Delete character before cursor.
dd Delete whole line.
dO Delete oject "O".
D Delete rest of line from cursor position.
J Join next line with current line.
NNJ Join next "NN" lines with current line.
<< Left shift line over "shiftwidth" chars.
>> Right shift line over "shiftwidth" chars.
~ Toggle case of alphabetic char under cursor.
Undo and redo
u Undo last change.
U Restore current line.
. Repeat last change.
& Repeat last substitute command.
Yank and Put
dd Delete whole line.
dO Delete object "O".
D Delete rest of line from cursor position.
NNdd Delete "NN" lines, starting with current line.
yy Yank whole line.
yO Yank object "O".
Y Yank Line.
NNY Yank "NN" lines, starting with current line.
p Put back after.
P Put back before or above.
"XD Delete line in buffer "X" ([a..z]).
"XdO Delete object "O" into buffer "X" ([a..z]).
"XY Yank line into buffer "X" ([a..z]).
"XyO Yank object "O" into buffer "X" ([a..z]).
"Xp Put back after from buffer "X" ([a..z]).
"XP Put back before from buffer "X" ([a..z]).
Pattern search expressions (if MAGIC set)
\ Quotes magic character literally.
^ Before PAT, matches PAT at begin of line.
$ After PAT, matches PAT at end of line.
. Matches any single character.
[charset] Matches any character in "charset".
[^charset] Matches any character not in "charset".
[a-z] Matches char in range between "a" and "z".
* Matches any number of the preceding.
\< Before PAT, matches PAT at begin of word.
\> After PAT, matches PAT at end of word.
Ex line addressing (for use in last line commands)
NN Line number "NN".
. Current line.
$ Last line.
+ Next line.
- Previous line.
+NN "NN" lines forward.
-NN "NN" lines backward.
NN+MM "MM" lines after line "NN".
NN-MM "MM" lines before line "NN".
NN,MM Range of lines between "NN" and "MM".
% Abbreviation of range "1,$".
Special ex commands (at last line, excute with RETURN)
:s /OLD/NEW/ Substitute first occurence of OLD with NEW.
:s /OLD/NEW/g Substitute all occurence of OLD with NEW.
:%s/OLD/NEW/g Substitute all occurence in all lines of OLD with NEW.
:m LL Move current line after line "LL".
:NN,MM m LL Move range of lines after line "LL".
:co LL Copy current line after line "LL".
:NN,MM co LL Copy range of lines after line "LL".
:d Delete current line.
:NN,MM d Delete range of lines.
:r file Read in file after current line.
:NN r file Read in file after line "NN".
Special options and flags
:set X Enable flag "X".
set NoX Disable flag "X".
:set X=value Set option "X" with "value".
:set X? Show value of option or flag.
:set all Show current settings of all options and flags.
:set Show changed options and flags.
vi options and flags
autoindent (ai)
autoprint (ap)
autowrite (aw)
beautify (bf)
directory (dir)
edcompatibility
errorbells (eb)
hardtabs (ht)
ignorecase (ic)
lisp
list
magic
mesg
number (nu)
open
optimize (op)
paragraphs (para)
prompt
readonly
redraw
remap
report
scroll
sections
shell (sh)
shiftwidth (sw)
showmatch (sm)
slowopen (slow)
tabstop (ts)
taglength (tl)
tags
term
terse
timeout
ttytype
warn
window
wrapscan (ws)
wrapmargin (wm)
writeany (wa)
Operators (work on objects)
c Change.
d Delete.
y Yank.
Objects (after operator)
w Current word.
b Word before cursor.
e End of next word.