vi

Cursor control and position

h or Left
j, or RETURNDown
k or Up
l, or SPACERight
wForward one word
bBack one word
eEnd of word
(Beginning of current sentence
)Beginning of next sentence
{Beginning of current paragraph
}Beginning of next paragraph
[[Beginning of current section
]]Beginning of next section
0Start of the current line
$End of the current line
^First non-white character on current line
+ or RETURNFirst character on next line
-First character on previous line
n|Character n of current line
HTop line of current screen
MMiddle line of current screen
Llast line of current screen
nHn lines after the top line of current screen
nLn lines before last line of current screen
CTRL+FForward one screen
CTRL+BBack one screen
CTRL+DDown half a screen
CTRL+UUp half a screen
CTRL+EDisplay another line at bottom of screen
CTRL+YDisplay another line at top of screen
z RETURNRedraw screen with cursor at top
z .Redraw screen with cursor in middle
z -Redraw screen with cursor at bottom
CTRL+LRedraw screen without re-positioning
CTRL+RRedraw screen without re-positioning
/testSearch for text (forward)
/Repeat forward search
?textSearch for text (backward)
?Repeat previous search backward
nRepeat previous search
NRepeat previous search, but in the opposite direction
/text/+nGo to line n after text
?text?-nGo to line n before text
%Find match of current parenthesis, brace, or bracket
CTRL+GDisplay line number of cursor
nGMove cursor to line number n
:nMove cursor to line number n
GMove to last line

Editing

aAppend after cursor
AAppend to end of current line
iInsert before cursor
IInsert at beginning of line
oOpen line above cursor
OOpen line below cursor
ESCEnd of insert mode
CTRL+IInsert a tab
CTRL+TMove to next tab position
BACKSPACEMove back one character
CTRL+UDelete current line
CTRL+VQuote next character
CTRL+WMove back one word
cwChange word
ccChange line
CChange from current position to end of line
ddDelete current line
ndd or dndDelete n lines
DDelete remainder of line
dwDelete word
d}Delete rest of paragraph
d^Delete back to start of line
c/patDelete up from first occurance of pat
dnDelete up to the next occurance of pattern
dfaDelete up to and including a on current line
dLDelete up to last line on screen
dGDelete to end of file
JJoin two lines
pInsert buffer after cursor
PInsert buffer before cursor
rxReplace the character with x
rtextReplace text beginning at cursor
sSubstittue cahracter
nsSubstitute n characters
SSubstitute entire line
uUndo last change
URestore current line
xDelete current cursor position
XDelete back one character
nXDelete previous n characters
.Repeat last change
~Reverse case
yCopy current line into new buffer
yyCopy current line
ywCopy word
y4w or 4ywCopy 4 words
y]]Copy up to next section heading
"xyyCopy current line into buffer x
"XdDelete and append to buffer x
"xpPut contents of buffer x

File Handling

:wWrite file
:w!Write file (ignoring warnings)
:w! fileOverwrite file (ignore warnings)
:wqWrite file and quit
:qQuit
:q!Quit (even if changes not saved)
:w fileWrite file as file, leaving original untouched
ZZQuit, only writing file if changed
:xQuit, only writing file if changed
:n1,n2 fileWrite lines n1 to n2 to file
:n1,n2 >> fileAppend lines n1 to n2 to file
:e file2edit file2 (current file becomes alternate file)
:e!Reload file from disk (revert to previous saved version)
:e#Edit alternate file
%Display current filename
#Display alternate filename
:nEdit next file
:n!Edit next file (ignore warnings)
:n filesSpecify new list of files
:x fileInsert file after cursor
:r !commandRun command and insert output after current line

Search and Replace

Change to normal mode with <ESC>. Search (Wraped around at end of file):
  Search STRING forward :   / STRING.
  Search STRING backward:   ? STRING.

  Repeat search:   n
  Repeat search in opposite direction:  N  (SHIFT-n)
Replace: Same as with sed, Replace OLD with NEW:
 
 First occurrence on current line:      :s/OLD/NEW
  
 Globally (all) on current line:        :s/OLD/NEW/g 

 Between two lines #,#:                 :#,#s/OLD/NEW/g
  
 Every occurrence in file:              :%s/OLD/NEW/g 

Links