VI Editor Complete Command Set

VI/VIM is often the only editor available on Linux servers. This page focuses on the commands engineers really use in production, with one-line explanations and examples.

Modes

ModePurpose
Normal modeNavigation and editing commands
Insert modeTyping text
Command modeSave, quit, search, replace

Open Files

CommandWhat it doesExample
vi file.txtOpens a file in vi.vi file.txt
vi +100 app.logOpens a file and jumps to line 100.vi +100 app.log
vi -R config.ymlOpens a file in read-only mode.vi -R config.yml

Insert Mode Commands

CommandWhat it doesExample
iStarts typing before the cursor.i
IStarts typing at the beginning of the line.I
aStarts typing after the cursor.a
AStarts typing at the end of the line.A
oCreates a new line below and enters insert mode.o
OCreates a new line above and enters insert mode.O
EscLeaves insert mode and returns to normal mode.Esc

Save and Exit

CommandWhat it doesExample
:wSaves the current file.:w
:qQuits the editor.:q
:wqSaves the file and quits.:wq
:xSaves and quits if changes exist.:x
:q!Quits without saving changes.:q!

Navigation

CommandWhat it doesExample
h j k lMoves left, down, up and right.j
ggJumps to the top of the file.gg
GJumps to the bottom of the file.G
:100Jumps to line 100.:100
Ctrl+dMoves half a page down.Ctrl+d
Ctrl+uMoves half a page up.Ctrl+u
0Moves to the start of the line.0
$Moves to the end of the line.$
wMoves to the next word.w
bMoves to the previous word.b

Search, Edit, Copy and Replace

CommandWhat it doesExample
/ERRORSearches forward for text./ERROR
?ERRORSearches backward for text.?ERROR
nMoves to the next search match.n
NMoves to the previous search match.N
ddDeletes the current line.dd
5ddDeletes five lines from the current line.5dd
dwDeletes the current word.dw
xDeletes one character.x
uUndoes the last change.u
Ctrl+rRedoes the undone change.Ctrl+r
yyCopies the current line.yy
5yyCopies five lines.5yy
pPastes below the cursor.p
PPastes above the cursor.P
rReplaces one character.r
:s/old/new/gReplaces text in the current line.:s/old/new/g
:%s/old/new/gReplaces text in the whole file.:%s/old/new/g
vStarts visual selection by character.v
VStarts visual selection by line.V

Minimal Survival Set

CommandWhat it doesExample
iStart typing.i
EscStop typing and return to command mode.Esc
:wqSave and exit.:wq
:q!Exit without saving.:q!
/textSearch for text./ERROR
nGo to next match.n
ddDelete a line.dd
yyCopy a line.yy
pPaste copied text.p