Vim is modal: Normal mode for commands, Insert mode for typing, Visual mode for selecting. Press Esc any time to return to Normal mode.
Click any command to copy it.
Get out (the most asked question)
| Command | What it does |
|---|
:q | Quit (fails if there are unsaved changes). |
:q! | Destructive: Quit and discard unsaved changes. |
:w | Save. |
:wq | Save and quit. |
ZZ | Save and quit (Normal mode). |
:w !sudo tee % | Save a file you opened without root permission. |
Modes
| Command | What it does |
|---|
i | Insert before the cursor. |
a | Insert after the cursor. |
o | Open a new line below and insert. |
O | Open a new line above. |
v | Visual mode: select characters. |
V | Visual mode: select lines. |
Ctrl-v | Visual block mode: select columns. |
Esc | Return to Normal mode. |
Move
| Command | What it does |
|---|
h j k l | Left, down, up, right. |
w / b | Next / previous word. |
0 / $ | Start / end of line. |
gg / G | Top / bottom of the file. |
:42 | Go to line 42. |
Ctrl-d / Ctrl-u | Scroll down / up half a page. |
% | Jump to the matching bracket. |
Edit
| Command | What it does |
|---|
x | Delete the character under the cursor. |
dd | Delete (cut) the line. |
yy | Copy the line. |
p / P | Paste after / before. |
u | Undo. |
Ctrl-r | Redo. |
. | Repeat the last change. |
cw | Change the word (delete it and enter insert mode). |
D | Delete to the end of the line. |
>> / << | Indent / unindent the line. |
ggVG | Select the whole file. |
Search and replace
| Command | What it does |
|---|
/pattern | Search forward. |
n / N | Next / previous match. |
* | Search for the word under the cursor. |
:%s/old/new/g | Replace all occurrences in the file. |
:%s/old/new/gc | Replace with confirmation for each. |
:noh | Clear search highlighting. |
Files and windows
| Command | What it does |
|---|
:e file | Open another file. |
:split / :vsplit | Split the window horizontally / vertically. |
Ctrl-w w | Switch between windows. |
:tabnew file | Open a file in a new tab. |
gt | Next tab. |
:set number | Show line numbers. |
:set paste | Paste text without auto-indent mangling. |
:!command | Run a shell command. |
Frequently asked questions
How do I exit Vim?
Press Esc, then type :q and Enter. To save and quit use :wq; to quit without saving use :q!.
Why does pasted text get indented strangely?
Auto-indent is re-indenting each line. Run :set paste before pasting, and :set nopaste afterwards.
More Linux & Shell cheat sheets