The Modes
- Normal mode: where Vim starts. Keys move the cursor and run commands; they do not type text.
- Insert mode: keys type text, as in any editor. Enter it with
i; leave it with Esc. - Visual mode: select text, then act on the selection. Enter it with
v. - Command-line mode: type
:in Normal mode, then a command such as:w, and press Enter.
When in doubt, press Esc once or twice: you are back in Normal mode. Commands are case sensitive, so u and U do different things.
Save and Quit
| Command | Meaning |
|---|---|
:w | Save |
:wq or :x | Save and quit |
ZZ | Save and quit |
:q | Quit if saved |
:q! | Quit, drop changes |
:w new.txt | Write a copy |
More, including files you may not write to, in how to save and quit in Vim.
Moving Around
| Key | Moves to |
|---|---|
h j k l | Left, down, up, right |
w / b | Next / previous word |
0 / $ | Line start / end |
gg / G | First / last line |
42G | Line 42 |
Ctrl+f / Ctrl+b | Page down / up |
% | Matching bracket |
Jumping to the end of a file is covered in how to go to the end of a file in Vim.
Editing
| Key | Meaning |
|---|---|
i / a | Insert before / after cursor |
I / A | Insert at line start / end |
o / O | New line below / above |
x | Delete character |
dd | Delete line |
dw | Delete word |
D | Delete to line end |
cw | Change word |
r | Replace one character |
u | Undo |
Ctrl+r | Redo |
See how to delete a line in Vim and how to undo and redo in Vim for the variations.
Copy and Paste
| Key | Meaning |
|---|---|
yy | Copy line |
yw | Copy word |
p / P | Paste after / before |
dd then p | Cut and paste a line |
Vim calls copying "yanking", and every delete also stores the text, so a delete works as a cut. Details in how to copy, cut, and paste in Vim.
Search and Replace
| Command | Meaning |
|---|---|
/error | Search forward |
?error | Search backward |
n / N | Next / previous match |
* | Next match of word |
:%s/old/new/g | Replace in file |
:%s/old/new/gc | Replace, confirm each |
Patterns and ranges are explained in how to search in Vim.
Files, Buffers, and Splits
| Command | Meaning |
|---|---|
:e file | Open a file |
:ls | List open buffers |
:bn / :bp | Next / previous buffer |
:split | Split horizontally |
:vsplit | Split vertically |
Ctrl+w w | Next window |
:q | Close window |
Counts and the Dot Command
Most Normal mode commands accept a number in front: 3dd deletes three lines, 5j moves five lines down, and 2yy copies two lines. The dot key . repeats your last change. Delete a word with dw, then press . to delete the next one; change a word with cw, then move to another and press . to make the same change.
Visual Mode
| Key | Meaning |
|---|---|
v | Select characters |
V | Select lines |
Ctrl+v | Select a block |
d / y | Delete / copy selection |
> / < | Indent / outdent |
Move the cursor with the usual keys to grow the selection, then press the action key. To practice all of this, run vimtutor, a short lesson inside Vim itself. It comes with the full Vim; on Debian and Ubuntu it is part of the vim-runtime package that the vim package installs, and the small vim-tiny package does not have it.