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.

Modes

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

Open Files

vi file.txt
vi +100 app.log
vi -R config.yml

Insert Mode

KeyBrief Description
iInsert before cursor
IInsert at beginning of line
aAppend after cursor
AAppend at end of line
oOpen new line below
OOpen new line above
EscLeave insert mode

Save and Exit

:w      save
:q      quit
:wq     save and quit
:x      save and quit
:q!     quit without saving

Navigation

h j k l        left down up right
gg             go to top
G              go to bottom
:100           go to line 100
Ctrl+d         half page down
Ctrl+u         half page up
0              start of line
$              end of line
w              next word
b              previous word

Search

/ERROR
?ERROR
n
N

Edit

dd      delete line
5dd     delete 5 lines
dw      delete word
x       delete character
u       undo
Ctrl+r  redo

Copy and Paste

yy      copy line
5yy     copy 5 lines
p       paste below
P       paste above

Replace

r
:s/old/new/g
:%s/old/new/g
:%s/localhost/127.0.0.1/g

Visual Mode

v       visual character mode
V       visual line mode
d       delete selection
y       copy selection

Very Practical Log Use

vi app.log
G
/Exception
n
:set number

Minimal Survival Set

i
Esc
:wq
:q!
/text
n
dd
yy
p