VI manaul

This manual is copied from here and has been modified by me.

Legend

default values1
<*>* must not be taken literally.
[*]* is optional.
^X<ctrl>X
<sp>space
<cr>carriage return
<lf>line feed
<ht>horizontal tab
<esc>escape
<erase>your erase character.
<kill>your kill character.
<intr>your interrupt character.
<a-z>an element in the range [a...z].
NWhether a number, N, can be prefixed ✔ to the command or not ❌.
CHARchar unequal to <ht> or <sp>
WORDword followed by <ht>, <sp>, or <lf>.

Move Commands

NCommandMeaning
h, ^H, <erase>Move N characters to the left.
j, <lf>, NN lines downward.
l, <sp>N chars to the right.
k, PN lines upward.
$To the end of line N from the cursor.
^To the first CHAR of the line.
_To the first CHAR N - 1 lines lower.
-To the first CHAR N lines higher.
+, <cr>To the first CHAR N lines lower.
0To the first char of the line.
|To column N (<ht>: only to the endpoint).
f<char>N <char>s to the right (find).
t<char>Till before N <char>s to the right.
F<char>N <char>s to the left.
T<char>Till after N <char>s to the left.
;Repeat latest ‘f’
,Idem in opposite direction.
wN words forward.
WN WORDS forward.
bN words backward.
BN WORDS backward.
eTo the end of word N forward.
ETo the end of WORD N forward.
GGo to line N (default EOF).
HTo line N from top of the screen (home).
LTo line N from bottom of the screen (last).
MTo the middle line of the screen.
)N sentences forward.
(N sentences backward.
}N paragraphs forward.
{N paragraphs backward.
]]To the next section (default EOF).
[[To the previous section (default begin of file).
`<a-z>To the mark.
'<a-z>To the first CHAR of the line with the mark.
``To the cursor position before the latest absolute jump (of which are examples ‘/’ and ‘G’).
''To the first CHAR of the line on which the cursor was placed before the latest absolute jump.
/<string>To the next occurrence of <string>.
?<string>To the previous occurrence of <string>.
nRepeat latest / or ? (next).
NIdem in opposite direction.
%Find the next bracket and go to its match (also with { or } and [ or ]).

Searching

CommandMeaning
:ta <name>Search in the tags file[s] where <name> is defined (file, line) and go to it.
^]Use the name under the cursor in a :ta command.
^TPop the previous tag off the tagstack and return to its position.
:[x,y]g/<string>/<cmd>Search globally [from line x to y] for <string> and execute the ‘ex’ <cmd> on each occurrence.
:[x,y]v/<string>/<cmd>Execute <cmd> on the lines that don’t match.

Undoing Changes

CommandMeaning
uUndo the latest change.
UUndo all changes on a line, while not having moved off of it.
:q!Quit vi without writing. (Meaning your changes won’t be saved)
:e!Re-edit a messed up file.

Appending Text

These take you into input mode, hit <esc> to reenter command mode.

NCommandMeaning
aN times after the cursor.
AN times at the end of line.
iN times before the cursor (insert) .
IN times before the first CHAR of the line.
oOn a new line below the current (open).
OOn a new line above the current.
><move>Shift lines N shiftwidths to the right, where <move> is a move command
>>Shift N lines down, one shiftwidth to the right.
["<a-zA-Z1-9>]pPut the contents of the (default undo) buffer N times after the cursor. A buffer containing lines is put only once, below the current line.
["<a-zA-Z1-9>]PPut the contents of the (default undo) buffer N times before the cursor. A buffer containing lines is put only once, above the current line.
.Repeat previous command N times. If the last command before a . command references a numbered buffer, the buffer number is incremented first (and the count is ignored):

Deleting Text

Everything deleted can be stored into a buffer. This is achieved by putting a " and a lowercase letter a-z before the delete command. The deleted text will be in the buffer with the used letter. If capital A-Z is used as the buffer name, the conjugate buffer, lowercase a-z will be appended instead of overwritten with the text. The undo buffer always contains the latest change. Buffers 1-9 contain the latest 9 LINE deletions, where "1 is the most recent.

NCommandMeaning
xDelete N chars under and after the cursor.
XDelete N chars before the cursor.
d<move>Delete N times in the direction of the <move>.
ddN lines.
DThe rest of the line.
<<move>Shift lines N shiftwidths to the left, where <move> is a move command
<<Shift N lines down one shiftwidth to the left.
.Repeat latest command N times.

Changing Text

These take you into input mode, hit <esc> to reenter command mode.

NCommandMeaning
r<char>Replace N chars by <char> - no <esc>.
ROverwrite the rest of the line, appending change N - 1 times.
sSubstitute N chars.
SSubstitute N lines.
c<move>Change from begin to endpoint of N<move>.
ccChange N lines.
CThe rest of the line and N - 1 next lines.
=<move>If the option ‘lisp’ is set, this command will realign the lines described by N<move> as though they had been typed with the option ‘ai’ set too.
~Switch lower and upper cases (should be an operator, like c).
JJoin N lines (default 2).
.Repeat latest command N times (‘J’ only once).
&Repeat latest ‘ex’ substitute command, e.g. ‘:s/wrong/good’.
:[x,y]s/<p>/<r>/<f>Substitute (on lines x through y) the pattern <p> (default the last pattern) with <r>. Useful flags <f> are ‘g’ for ‘global’ (i.e. change every non-overlapping occurrence of <p>) and ‘c’ for ‘confirm’ (type ‘y’ to confirm a particular substitution, else <cr>). Instead of ‘/’ any punctuation CHAR unequal to <lf> can be used as delimiter.

Substitute Replacement Patterns

The basic meta-characters for the replacement pattern are ‘&’ and ‘~‘; these are given as ‘&’ and ‘~’ when nomagic is set. Each instance of ‘&’ is replaced by the characters which the regular expression matched. The meta-character ‘~’ stands, in the replacement pattern, for the defining text of the previous replacement pattern. Other meta-sequences possible in the replacement pattern are always introduced by the escaping character ‘\‘. The sequence ‘\n’ (with ‘n’ in [1-9]) is replaced by the text matched by the n-th regular subexpression enclosed between ‘(’ and ‘)’. The sequences ‘\u’ and ‘\l’ cause the immediately following character in the replacement to be converted to upper- or lower-case respectively if this character is a letter. The sequences ‘\U’ and ‘\L’ turn such conversion on, either until ‘\E’ or ‘\e’ is encountered, or until the end of the replacement pattern.

Remembering (Copying) Text

With yank commands you can put "<a-zA-Z> before the command, just as with delete commands. Otherwise you only copy to the undo buffer. The use of buffers <a-z> is the way of copying text to another file; see the :e <file> command.

NCommandMeaning
y<move>Yank from begin to endpoint of N<move>.
yyN lines.
YIdem (should be equivalent to y$ though).
m<a-z>Mark the cursor position with a letter.

Commands While In Append or Change Mode

CommandMeaning
^@If typed as the first character of the insertion, it is replaced with the previous text inserted (max. 128 chars), after which the insertion is terminated.
^VDeprive the next char of its special meaning (e.g. <esc>).
^DOne shiftwidth to the left, but only if nothing else has been typed on the line.
0^Remove all indentation on the current line (there must be no other chars on the line).
^^Idem, but it is restored on the next line.
^TOne shiftwidth to the right, but only if nothing else has been typed on the line.
^H or <erase>One char back.
^WOne word back.
<kill>Back to the beginning of the change on the current line.
<intr>Like <esc> (but you get a beep as well).