vi Command Cheat Sheet
vi is a screen-oriented text editor originally created for the Unix operating system.
Note: On most modern Linux systems, vi is a symlink to vim (Vi IMproved) or elvis.
Modes
- Command Mode (Default): For navigation and manipulation. Press
Esc to return here.
- Insert Mode: For typing text. Press
i, a, o to enter.
- Last Line Mode: For saving/exiting. Press
: from Command Mode.
Basic Workflow
- Open file:
vi file.txt
- Enter Insert Mode: Press
i
- Type text...
- Exit Insert Mode: Press
Esc
- Save and Exit: Type
:wq and Enter
Navigation (Command Mode)
| Key |
Action |
h |
Left |
j |
Down |
k |
Up |
l |
Right |
w |
Jump forward to start of word |
b |
Jump backward to start of word |
0 |
Start of line |
$ |
End of line |
gg |
Go to first line |
G |
Go to last line |
10G |
Go to line 10 |
Editing
| Key |
Action |
i |
Insert at cursor |
a |
Append after cursor |
A |
Append at end of line |
o |
Open new line below |
O |
Open new line above |
x |
Delete character at cursor |
dd |
Delete current line |
yy |
Yank (copy) line |
p |
Paste after cursor |
u |
Undo |
Ctrl+r |
Redo |
Search
| Command |
Action |
/text |
Search forward for "text" |
?text |
Search backward for "text" |
n |
Next match |
N |
Previous match |
Saving and Quitting
| Command |
Action |
:w |
Save (Write) |
:q |
Quit |
:wq |
Save and Quit |
:x |
Save and Quit (Same as :wq) |
:q! |
Quit without saving (Force) |