VI (Visual Editor) is one of the most ubiquitous text editors in the UNIX/Linux world. Created by Bill Joy in 1976, it's available on virtually every UNIX-like system you'll encounter. Whether you're editing configuration files on a remote server, working in a minimal environment, or just want a fast, keyboard-driven editor, VI is an essential tool in your sysadmin toolkit.
The most important concept in VI is that it operates in different modes. This is what confuses most beginners, but once you understand it, everything else makes sense.
This is the default mode when you start VI. In this mode, keys perform commands rather than inserting text. You can:
How to get here: Press ESC from any other mode
In this mode, VI behaves like a normal text editor - what you type appears on screen. You enter text and make changes.
How to get here: Press i, a, o, or other insert commands from Command Mode
How to leave: Press ESC to return to Command Mode
This mode is for entering commands that appear at the bottom of the screen. You use it to save files, quit, search and replace, and more.
How to get here: Press : from Command Mode
How to leave: Press ESC or complete your command and press ENTER
ESC. It always takes you back to Command Mode, your safe home base.
This is famously confusing for beginners. Here are all the ways to exit:
| Command | What It Does |
|---|---|
:q |
Quit (fails if you have unsaved changes) |
:q! |
Quit without saving (discard changes) |
:w |
Write (save) the file |
:wq |
Write and quit |
:x |
Write and quit (only writes if changes were made) |
ZZ |
Save and quit (from Command Mode, no colon needed) |
:w newfile |
Save as newfile |
vi test.txt, type :q and press Enter to exit immediately.
| Key | Movement |
|---|---|
h |
Left one character |
j |
Down one line |
k |
Up one line |
l |
Right one character |
Memory Aid: Think of j as pointing down (it has a descender), k pointing up. Or just use arrow keys until you're comfortable.
| Key | Movement |
|---|---|
w |
Forward to beginning of next word |
b |
Backward to beginning of word |
e |
Forward to end of word |
W |
Forward to next word (whitespace-delimited) |
B |
Backward by word (whitespace-delimited) |
| Key | Movement |
|---|---|
0 |
Beginning of current line |
^ |
First non-whitespace character of line |
$ |
End of current line |
+ or Enter |
First character of next line |
- |
First character of previous line |
| Key | Movement |
|---|---|
H |
Top of screen (High) |
M |
Middle of screen |
L |
Bottom of screen (Low) |
Ctrl+f |
Forward one screen |
Ctrl+b |
Backward one screen |
Ctrl+d |
Down half screen |
Ctrl+u |
Up half screen |
G |
Go to last line of file |
gg or 1G |
Go to first line of file |
42G |
Go to line 42 |
h, j, k, l. Then try w and b to move by words. Press 0 and $ to jump to line start/end.
There are several ways to enter Insert Mode from Command Mode. Each positions your cursor differently:
| Key | Action |
|---|---|
i |
Insert before cursor |
a |
Append after cursor |
I |
Insert at beginning of line |
A |
Append at end of line |
o |
Open new line below current line |
O |
Open new line above current line |
ESC when you're done inserting text to return to Command Mode!
vi practice1.txti to enter Insert ModeESC to return to Command Modeo to open a new line belowESCO (capital) to open a line aboveESC:wq| Command | Action |
|---|---|
x |
Delete character under cursor |
X |
Delete character before cursor |
dw |
Delete word |
dd |
Delete entire line |
D |
Delete from cursor to end of line |
d$ |
Delete to end of line (same as D) |
d0 |
Delete from cursor to beginning of line |
The c command is like delete, but it puts you in Insert Mode after deleting:
| Command | Action |
|---|---|
cw |
Change word (delete word and enter Insert Mode) |
cc |
Change entire line |
C |
Change from cursor to end of line |
s |
Substitute character (delete char and insert) |
S |
Substitute line (same as cc) |
| Command | Action |
|---|---|
u |
Undo last change |
U |
Undo all changes to current line |
Ctrl+r |
Redo (vim only, not traditional vi) |
dw to delete it. Press u to undo. Move to a line and press dd to delete it, then u to bring it back.
In VI, copying is called "yanking" and uses the y command. Pasting uses p.
| Command | Action |
|---|---|
yy |
Yank (copy) current line |
yw |
Yank word |
y$ |
Yank to end of line |
y0 |
Yank to beginning of line |
| Command | Action |
|---|---|
p |
Put after cursor or below current line |
P |
Put before cursor or above current line |
dd, dw, etc., that text is automatically placed in a buffer. You can paste it with p. So dd followed by p is effectively a "cut and paste" operation.
yy to yank itp to paste belowdd to delete itp to paste it:q!Searching is one of the most useful features in VI.
| Command | Action |
|---|---|
/pattern |
Search forward for pattern |
?pattern |
Search backward for pattern |
n |
Repeat search in same direction |
N |
Repeat search in opposite direction |
| Command | Action |
|---|---|
fx |
Find next occurrence of character x on line |
Fx |
Find previous occurrence of character x on line |
; |
Repeat last f or F command |
, |
Repeat last f or F command in opposite direction |
vi /etc/passwd. Type /root and press Enter to find "root". Press n to find next occurrence. Press :q to exit.
You can precede almost any command with a number to repeat it. This is extremely powerful!
gg3dd5j4yyGpRemember, you enter Last Line Mode by pressing : from Command Mode.
| Command | Action |
|---|---|
:w |
Write (save) file |
:w filename |
Save as filename |
:q |
Quit |
:q! |
Quit without saving |
:wq |
Write and quit |
:x |
Write (if changes) and quit |
:e filename |
Edit another file |
:r filename |
Read filename and insert below cursor |
:!command |
Execute shell command |
:set number |
Show line numbers |
:set nonumber |
Hide line numbers |
Print or save this for quick reference!
i:q! will always get you outcp /etc/hosts ~/hosts.backupvi ~/hosts.backupGo# Test entryESCo then type: 192.168.1.100 testserverESC:wqcat ~/hosts.backupCongratulations! You now know the basics of VI. You can:
The next tutorial will cover the commands you'll use 80% of the time - the efficient, powerful shortcuts that make VI users so fast.
Created for Linux System Administrators | Part 1 of the VI Learning Series
Remember: When in doubt, press ESC!