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
| Mode | Purpose |
|---|---|
| Normal mode | Navigation and editing commands |
| Insert mode | Typing text |
| Command mode | Save, quit, search, replace |
Open Files
| Command | What it does | Example |
|---|---|---|
vi file.txt | Opens a file in vi. | vi file.txt |
vi +100 app.log | Opens a file and jumps to line 100. | vi +100 app.log |
vi -R config.yml | Opens a file in read-only mode. | vi -R config.yml |
Insert Mode Commands
| Command | What it does | Example |
|---|---|---|
i | Starts typing before the cursor. | i |
I | Starts typing at the beginning of the line. | I |
a | Starts typing after the cursor. | a |
A | Starts typing at the end of the line. | A |
o | Creates a new line below and enters insert mode. | o |
O | Creates a new line above and enters insert mode. | O |
Esc | Leaves insert mode and returns to normal mode. | Esc |
Save and Exit
| Command | What it does | Example |
|---|---|---|
:w | Saves the current file. | :w |
:q | Quits the editor. | :q |
:wq | Saves the file and quits. | :wq |
:x | Saves and quits if changes exist. | :x |
:q! | Quits without saving changes. | :q! |
Navigation
| Command | What it does | Example |
|---|---|---|
h j k l | Moves left, down, up and right. | j |
gg | Jumps to the top of the file. | gg |
G | Jumps to the bottom of the file. | G |
:100 | Jumps to line 100. | :100 |
Ctrl+d | Moves half a page down. | Ctrl+d |
Ctrl+u | Moves half a page up. | Ctrl+u |
0 | Moves to the start of the line. | 0 |
$ | Moves to the end of the line. | $ |
w | Moves to the next word. | w |
b | Moves to the previous word. | b |
Search, Edit, Copy and Replace
| Command | What it does | Example |
|---|---|---|
/ERROR | Searches forward for text. | /ERROR |
?ERROR | Searches backward for text. | ?ERROR |
n | Moves to the next search match. | n |
N | Moves to the previous search match. | N |
dd | Deletes the current line. | dd |
5dd | Deletes five lines from the current line. | 5dd |
dw | Deletes the current word. | dw |
x | Deletes one character. | x |
u | Undoes the last change. | u |
Ctrl+r | Redoes the undone change. | Ctrl+r |
yy | Copies the current line. | yy |
5yy | Copies five lines. | 5yy |
p | Pastes below the cursor. | p |
P | Pastes above the cursor. | P |
r | Replaces one character. | r |
:s/old/new/g | Replaces text in the current line. | :s/old/new/g |
:%s/old/new/g | Replaces text in the whole file. | :%s/old/new/g |
v | Starts visual selection by character. | v |
V | Starts visual selection by line. | V |
Minimal Survival Set
| Command | What it does | Example |
|---|---|---|
i | Start typing. | i |
Esc | Stop typing and return to command mode. | Esc |
:wq | Save and exit. | :wq |
:q! | Exit without saving. | :q! |
/text | Search for text. | /ERROR |
n | Go to next match. | n |
dd | Delete a line. | dd |
yy | Copy a line. | yy |
p | Paste copied text. | p |