Vim cheat sheet
Every Vim command you need on one page: moving around, editing, copy and paste, search and replace, and how to save and exit. Search it or filter by group.
Stuck? Press Esc, then type :q! and Enter
160 commands
Save and exit
:w | Save the file |
|---|---|
:w name.txt | Save as a new file name |
:q | Quit (refuses if there are unsaved changes) |
:q! | Quit and throw away unsaved changes |
:wq | Save and quit |
:x | Save (only if changed) and quit |
ZZ | Save and quit, same as :x |
ZQ | Quit without saving, same as :q! |
:wa | Save all open files |
:qa | Quit all windows |
:wqa | Save all files and quit |
:qa! | Quit everything without saving |
:w !sudo tee % | Save a file you opened without permission to edit it |
Modes
Esc | Back to Normal mode (press it whenever you are unsure) |
|---|---|
i | Insert mode, to type text |
v | Visual mode, to select characters |
V | Visual line mode, to select whole lines |
Ctrl+v | Visual block mode, to select a column |
: | Command-line mode, for commands such as :w |
R | Replace mode: typing overwrites text |
Move the cursor
h j k l | Left, down, up, right (arrow keys also work) |
|---|---|
w | Start of the next word |
b | Start of the previous word |
e | End of the word |
W B E | Same as w b e, but words are split only by spaces |
0 | Start of the line |
^ | First non-blank character of the line |
$ | End of the line |
gg | First line of the file |
G | Last line of the file |
:42 or 42G | Go to line 42 |
Ctrl+d | Down half a screen |
Ctrl+u | Up half a screen |
Ctrl+f | Down a full screen |
Ctrl+b | Up a full screen |
} | Next paragraph or block |
{ | Previous paragraph or block |
% | Jump to the matching bracket ( ) [ ] { } |
H M L | Top, middle, bottom of the screen |
zz | Scroll so the cursor line is in the middle |
fx | Jump to the next x on the line |
tx | Jump to just before the next x |
; | Repeat the last f or t jump |
Ctrl+o | Jump back to where you were |
Ctrl+i | Jump forward again |
Insert text
i | Insert before the cursor |
|---|---|
a | Insert after the cursor |
I | Insert at the start of the line |
A | Insert at the end of the line |
o | New line below, and insert |
O | New line above, and insert |
ea | Insert at the end of the word |
Ctrl+w | In Insert mode: delete the word before the cursor |
Ctrl+n | In Insert mode: autocomplete the word |
Esc | Stop inserting |
Edit
x | Delete the character under the cursor |
|---|---|
X | Delete the character before the cursor |
r | Replace one character (r then the new letter) |
cw | Change to the end of the word |
ciw | Change the whole word under the cursor |
ci" | Change the text inside quotes |
cc or S | Change the whole line |
C | Change to the end of the line |
s | Delete the character and start inserting |
J | Join the next line onto this one |
~ | Swap upper and lower case |
gUU | Make the line UPPERCASE |
guu | Make the line lowercase |
>> | Indent the line |
<< | Remove one indent |
== | Fix the indent of the line |
gg=G | Fix the indent of the whole file |
xp | Swap two characters |
ddp | Swap two lines |
Cut, copy and paste
yy | Copy (yank) the line |
|---|---|
3yy | Copy 3 lines |
yw | Copy to the end of the word |
yiw | Copy the word under the cursor |
y$ | Copy to the end of the line |
ggyG | Copy the whole file |
dd | Cut (delete) the line |
3dd | Cut 3 lines |
dw | Cut to the end of the word |
diw | Cut the word under the cursor |
D or d$ | Cut to the end of the line |
dG | Cut to the end of the file |
dgg | Cut to the start of the file |
:%d | Delete every line in the file |
p | Paste after the cursor (below, for lines) |
P | Paste before the cursor (above, for lines) |
"+y | Copy to the system clipboard (then a motion, or in Visual mode) |
"+p | Paste from the system clipboard |
Visual mode
v V Ctrl+v | Select by character, line or block |
|---|---|
ggVG | Select all |
o | Jump to the other end of the selection |
aw | Select a word |
ib | Select inside ( ) brackets |
iB | Select inside { } braces |
y | Copy the selection |
d | Cut the selection |
c | Change the selection |
> < | Indent or unindent the selection |
u U | Make the selection lower or UPPER case |
gv | Select the last selection again |
Ctrl+v, I, text, Esc | Type the same text at the start of many lines |
Search and replace
/word | Search forward for word |
|---|---|
?word | Search backward |
n | Next match |
N | Previous match |
* | Search for the word under the cursor |
# | Same, searching backward |
/\cword | Search ignoring upper and lower case |
:noh | Clear the search highlighting |
:s/old/new/ | Replace the first old on this line |
:s/old/new/g | Replace every old on this line |
:%s/old/new/g | Replace every old in the file |
:%s/old/new/gc | Replace in the file, asking each time (y / n / a / q) |
:%s/old/new/gi | Replace in the file, ignoring case |
:10,20s/old/new/g | Replace on lines 10 to 20 only |
:g/word/d | Delete every line containing word |
:v/word/d | Delete every line not containing word |
Undo and repeat
u | Undo |
|---|---|
Ctrl+r | Redo |
U | Undo all changes on the last edited line |
. | Repeat the last change |
:earlier 5m | Go back to how the file was 5 minutes ago |
Files, windows and tabs
:e file | Open a file |
|---|---|
:e! | Reload the file, throwing away changes |
:Ex | Browse files in the current folder |
:ls | List open files (buffers) |
:bn :bp | Next or previous open file |
:sp file | Split the window horizontally |
:vs file | Split the window vertically |
Ctrl+w w | Move to the next window |
Ctrl+w h/j/k/l | Move to the window left, below, above, right |
Ctrl+w q | Close the window |
:only | Close every other window |
:tabnew file | Open a file in a new tab |
gt gT | Next or previous tab |
:tabclose | Close the tab |
Macros and marks
qa | Start recording a macro into register a |
|---|---|
q | Stop recording |
@a | Play macro a |
@@ | Play the last macro again |
10@a | Play macro a 10 times |
ma | Set mark a at the cursor |
'a | Jump to the line of mark a |
`a | Jump to the exact spot of mark a |
:marks | List marks |
:reg | Show what is in each register (copy slot) |
Settings and help
:set number | Show line numbers (:set nu) |
|---|---|
:set relativenumber | Show line numbers relative to the cursor |
:set nonumber | Hide line numbers |
:syntax on | Turn on syntax colouring |
:set ignorecase smartcase | Searches ignore case unless you type a capital |
:set hlsearch | Highlight all search matches |
:set wrap / nowrap | Wrap long lines, or not |
:set expandtab tabstop=4 shiftwidth=4 | Indent with 4 spaces instead of tabs |
:help word | Built-in help, e.g. :help :s |
vimtutor | Run in your terminal for a 30-minute interactive lesson |
Ctrl+d means hold Ctrl and press d. gg means press g twice. Upper and lower case are different commands.
How to exit Vim (save or quit)
- Press Esc to get back to Normal mode. Press it twice if you are not sure.
- Type one of the commands below. It appears at the bottom of the screen.
- Press Enter.
| Command | What it does |
|---|---|
| :wq | Save and quit |
| :q! | Quit without saving: your changes are lost |
| :q | Quit; Vim refuses if you have unsaved changes |
| :w | Save and keep editing |
| ZZ | Save and quit, no Enter needed |
If :wq just types text into your file, you are still in Insert mode: press Esc first. If Vim says E37: No write since last change, it is protecting unsaved work; use :wq to keep it or :q! to drop it. If you opened Vim by accident from git, :wq accepts the commit message and :cq cancels.
Vim modes explained
Vim is confusing at first because the same key does different things depending on the mode. In Normal mode, dd deletes a line; in Insert mode, it types "dd". Vim starts in Normal mode, and Esc always takes you back there.
| Mode | Get there with | Use it to |
|---|---|---|
| Normal | Esc | Move around, delete, copy, paste, undo |
| Insert | i, a, o | Type text like a normal editor |
| Visual | v, V, Ctrl+v | Select text, then copy, cut or indent it |
| Command-line | : | Save, quit, search and replace, change settings |
The bottom-left corner shows -- INSERT -- or -- VISUAL --; if it shows nothing, you are in Normal mode.
The 15 commands to learn first
You can edit any file with just these. Learn the rest when you need them.
| Keys | What it does |
|---|---|
| i | Start typing |
| Esc | Stop typing |
| h j k l | Move left, down, up, right |
| w / b | Next word / previous word |
| 0 / $ | Start / end of the line |
| gg / G | Top / bottom of the file |
| x | Delete a character |
| dd | Delete (cut) a line |
| yy | Copy a line |
| p | Paste below |
| u | Undo |
| Ctrl+r | Redo |
| /word | Find word (n for the next one) |
| :w | Save |
| :q | Quit |
To practise, run vimtutor in a terminal. It comes with Vim and walks you through the basics in about 30 minutes.
How to search and replace in Vim
To find text, press /, type the word and press Enter; n goes to the next match and N to the previous one. To replace, use the substitute command:
% means the whole file (leave it out for just the current line), s is substitute, and g replaces every match on a line instead of only the first. Add c to be asked before each change, or i to ignore case.
Example: a config file says colour in 12 places, but one of them is a filename you must not change.
Step 1: type the command and press Enter
Step 2: answer at each match
Result: press y eleven times and n on the filename. Vim reports 11 substitutions on 11 lines. If you get it wrong, u undoes the whole replace in one go.
How Vim commands combine
Most editing commands are an action plus a movement. d (delete), c (change) and y (copy) wait for you to say how far: dw deletes to the next word, d$ to the end of the line. Put a number in front to repeat: 3dd deletes three lines. Once you know a few of each, you know dozens of commands.
| Then… | d (delete) | c (change) | y (copy) |
|---|---|---|---|
| w: to the next word | dw | cw | yw |
| iw: the whole word | diw | ciw | yiw |
| $: to the end of the line | d$ | c$ | y$ |
| i": inside quotes | di" | ci" | yi" |
| same key: the whole line | dd | cc | yy |
| G: to the end of the file | dG | cG | yG |
Copy and paste with the system clipboard
y and p use Vim's own clipboard, so text you copy in Vim will not paste into your browser. To use the normal clipboard, put "+ in front: "+yy copies a line, "+p pastes. To copy a whole file, use gg"+yG.
This needs a Vim built with clipboard support: run vim --version and look for +clipboard. If it says -clipboard, install a fuller package (such as vim-gtk3 on Ubuntu) or use Neovim. In a terminal you can also paste with the terminal's own shortcut while in Insert mode.
Frequently asked questions
How do I select all in Vim?
Type ggVG: gg goes to the top, V starts selecting lines and G extends to the bottom. Then y copies, d deletes, or "+y copies to the system clipboard.
How do I show line numbers in Vim?
Type :set number and press Enter. :set nonumber hides them. To keep them on, add the line set number to the file ~/.vimrc.
How do I go to a specific line?
Type :42 and press Enter, or 42G, to jump to line 42. gg goes to the first line and G to the last.
How do I delete everything in a file?
Type :%d and press Enter, or ggdG. Press u straight away if you change your mind.
Sources
- Vim help files (vimhelp.org): the official Vim documentation, the same as :help inside Vim.
- Vim help: :substitute: search and replace and its flags.
- vim.org: documentation: vimtutor and the user manual.
Related Tools
Build and preview Valorant crosshair settings.
Turn JSON rows into bar, line, or pie charts.
Convert WebP files and Base64 data in both directions.
Convert GIF files and Base64 data in both directions.
Save any GIF frame, or all frames, as JPG.
Save any GIF frame, or all frames, as PNG.