What it does
vim is on almost every Linux server (vi is the small version). It has modes:
- Normal mode (command mode): keys are commands: move, delete, copy, paste. vim starts here.
- Insert mode: keys type text. Enter with
i,aoro; leave withEsc. - Command-line mode: commands after
:like:w(save) or:q(quit), and searches after/. - Visual mode: select text with
v(characters),V(lines) orCtrl+V(block).
Most commands can take a number first: 5dd deletes 5 lines, 3w moves 3 words.
Learn the basics in 30 minutes with vimtutor.
How to write it
vim [OPTIONS] [FILE ...]
vim +NUMBER FILE
vim -d FILE1 FILE2Main parts
Move (normal mode)
hjkl- Left, down, up, right (arrow keys also work).
wbe- Next word, back one word, end of the word.
0^$- Start of the line, first character, end of the line.
ggG- First line, last line. 25G (or :25) goes to line 25.
Ctrl+FCtrl+B- One page down, one page up.
%- Jump to the matching bracket.
Insert
ia- Insert before the cursor / after the cursor.
IA- Insert at the start / at the end of the line.
oO- New line below / above, in insert mode.
Esc- Back to normal mode.
Edit
x- Delete the character under the cursor.
dd- Delete (cut) the line. 5dd = 5 lines.
dwd$- Delete to the next word / to the end of the line.
cwcc- Change a word / change the whole line.
yyyw- Copy (yank) the line / a word.
pP- Paste after / before the cursor.
uCtrl+R- Undo / redo.
.- Repeat the last change.
r- Replace one character.
>><<- Indent / unindent the line.
J- Join the next line to this one.
Search and replace
/text?text- Search forward / backward.
nN- Next / previous match.
*- Search the word under the cursor.
:s/old/new/- Replace the first "old" in this line.
:%s/old/new/g- Replace every "old" in the whole file.
:%s/old/new/gc- Replace everywhere, but ask each time.
Files and quitting
:w- Save.
:w newnamesaves to another file. :q- Quit (only when nothing changed).
:wq:xZZ- Save and quit.
:q!- Quit and throw the changes away.
:w!sudotee%- Save a file you opened without root (asks the sudo password).
:efile- Open another file.
:rfile- Read (insert) a file under the cursor.
Settings
:setnumber- Show line numbers (
:set nonumberto hide). :setpaste- Paste text without automatic indent.
:syntaxon- Colors for code.
:setlist- Show tabs and line ends (find hidden characters).
Options 28
Most used
Nothing matches this filter.
Examples 12
$ run as a normal user# run as root (or with sudo)
1Open a file and save after an edit
vim /etc/hosts (press i, type your line, press Esc, then type :wq and Enter)The basic cycle: i → type → Esc → :wq.
2Leave without saving when you made a mistake
vim /etc/fstab (press Esc, then type :q! and Enter):q!throws all changes away. Your safety exit.3Open a file at a line number from an error message
vim +42 /etc/httpd/conf/httpd.confError messages often say "line 42". Jump straight there.
4Replace a word in the whole file
vim /etc/ssh/sshd_config :%s/^#PermitRootLogin yes/PermitRootLogin no/^anchors the match at the line start. Then:wq.5Look at a file without changing it
vim -R /var/log/messagesRead-only: no risk of saving by mistake. Same as
view.6Compare two config files side by side
vim -d /etc/ssh/sshd_config /etc/ssh/sshd_config.rpmnewDifferences are colored.
]cjumps to the next difference,dotakes the other side's version.7Save a file you opened without sudo
vim /etc/motd :w !sudo tee % > /dev/null%means the current file. Type the sudo password, then:q!to leave.8Comment out many lines at once
vim app.conf (Ctrl+V, move down with j, press I, type #, press Esc)Block insert puts
#before every selected line.9Show hidden tabs and spaces in a YAML file
vim deploy.yaml :set listYAML breaks on tabs.
:set listshows them as^I.10Recover a file after a crash or a lost SSH session
vim -r notes.txtvim keeps a swap file (
.notes.txt.swp). Delete the swap file after a good recovery.11Run vim commands in a script (no screen)
vim -Es -c '%s/8080/9090/g' -c 'wq' app.confBatch edit without opening the editor. For simple jobs
sed -iis easier.12Learn vim in 30 minutes
vimtutorA lesson file you edit while you learn. Do it once; it pays back every day.
Nothing matches this filter.
Watch out
- In insert mode,
:wqis just text. PressEscfirst. - "E37: No write since last change" means you tried
:qwith unsaved changes. Use:wqor:q!. - "Found a swap file" means the file is open elsewhere or vim crashed. Choose (R)ecover or (Q)uit, never edit twice at the same time.
- Use
visudofor /etc/sudoers andcrontab -efor crontabs, not plain vim.
Files
~/.vimrc- Your vim settings (like
set number). /etc/vimrc- System-wide settings.
~/.viminfo- History of commands, searches and marks.
Tips to remember
- Lost?
Esc Esc :q!. - Remember the grammar: number + verb + move:
d3w= delete 3 words,y$= copy to the end of the line. .repeats the last change. It saves a lot of typing.