79 entries · 7 sections
Vim Cheat Sheet
Vim and Neovim commands organised by mode and by task: moving, editing, yanking, searching, replacing, and managing files, buffers, splits and macros. Enough to be productive without reading the manual, and pointers to the parts of the manual worth reading.
Modes
| Syntax | What it does |
|---|---|
Esc | Return to Normal mode from anywhere. When you are lost, press this first. |
i / a | Insert before the cursor / after the cursor. |
I / A | Insert at the first non-blank character of the line / at the end of the line. |
o / O | Open a new line below / above and start inserting. |
v | Visual mode: select characters. |
V | Visual Line mode: select whole lines. |
Ctrl-v | Visual Block mode: select a rectangle. The way to edit a column. |
R | Replace mode: overwrite characters as you type. |
: | Command-line mode, for :w, :q, :s and everything else. |
Moving around
| Syntax | What it does |
|---|---|
h j k l | Left, down, up, right. |
w / b | Forward to the next word start / back to the previous one. |
e / ge | End of this word / end of the previous word. |
0 / ^ / $ | Start of the line / first non-blank character / end of the line. |
gg / G | Top of the file / bottom of the file. |
42G or :42 | Jump straight to line 42. |
{ / } | Previous / next paragraph, meaning the next blank line. |
Ctrl-d / Ctrl-u | Half a screen down / up. |
Ctrl-f / Ctrl-b | A full screen forward / back. |
f x / F x | Jump to the next / previous x on this line. |
t x / T x | Jump to just before the next x / just after the previous one. |
; / , | Repeat the last f or t forwards / backwards. |
% | Jump to the matching bracket, brace or parenthesis. |
Ctrl-o / Ctrl-i | Back / forward through your jump history, across files. |
Editing and text objects
| Syntax | What it does |
|---|---|
x / X | Delete the character under / before the cursor. |
dd | Delete the current line. |
3dd | Delete three lines. Almost every command takes a count like this. |
dw / d$ / d0 | Delete to the next word / to the end of the line / to the start of the line. |
dG / dgg | Delete to the end / to the start of the file. |
cw / cc | Change a word / the whole line, dropping you into Insert mode. |
ciw | Change the word the cursor is inside, wherever in the word it sits. |
ci" / ci( | Change everything inside the quotes / the parentheses. |
da( | Delete inside the parentheses and the parentheses themselves. |
r x | Replace one character with x without leaving Normal mode. |
J | Join this line with the next one. |
>> / << | Indent / outdent the current line. |
. | Repeat the last change. The single most valuable key in Vim. |
u / Ctrl-r | Undo / redo. |
Copy and paste
| Syntax | What it does |
|---|---|
yy | Yank (copy) the current line. |
3yy | Yank three lines. |
yw / y$ | Yank a word / to the end of the line. |
p / P | Put after / before the cursor. |
dd then p | Cut a line and paste it elsewhere — how you move a line. |
"ayy then "ap | Yank into the named register a, then paste from it. |
"+y / "+p | Yank to / paste from the system clipboard, on a build with clipboard support. |
:reg | Show every register and what is currently in it. |
Search and replace
| Syntax | What it does |
|---|---|
/text | Search forwards for text. |
?text | Search backwards. |
n / N | Next / previous match. |
* / # | Search for the word under the cursor, forwards / backwards. |
:%s/old/new/g | Replace every occurrence in the file. |
:%s/old/new/gc | The same, but confirm each replacement. |
:s/old/new/g | Replace only on the current line. |
:10,20s/old/new/g | Replace within a range of lines. |
:%s/\<word\>/new/g | Replace whole words only. |
:noh | Clear the leftover search highlighting. |
:g/pattern/d | Delete every line that matches a pattern. |
:v/pattern/d | Delete every line that does not match. |
Files, buffers, splits and tabs
| Syntax | What it does |
|---|---|
:w / :w file | Save / save as. |
:q / :q! | Quit / quit discarding unsaved changes. |
:wq or :x or ZZ | Save and quit. |
:qa! | Quit every window, discarding changes. |
:e file | Open another file. |
:e! | Reload the current file from disk, throwing away your changes. |
:ls | List the open buffers. |
:bn / :bp / :b3 | Next buffer / previous buffer / buffer number three. |
:sp file / :vsp file | Open a file in a horizontal / vertical split. |
Ctrl-w w / Ctrl-w h j k l | Cycle through windows / move to the window in that direction. |
Ctrl-w q / Ctrl-w o | Close this window / close every window except this one. |
:tabnew file, gt, gT | Open a tab, go to the next tab, go to the previous tab. |
Worth knowing
| Syntax | What it does |
|---|---|
:set number / :set relativenumber | Show absolute line numbers / distances from the cursor, which makes counts easy. |
:set ignorecase smartcase | Search case-insensitively unless you type a capital letter. |
:set paste | Stop auto-indent mangling pasted text. Not needed in Neovim, which handles bracketed paste. |
:set spell | Turn on spell checking; z= suggests corrections for the word under the cursor. |
qa ... q then @a | Record a macro into register a, then replay it. @@ replays the last one again. |
Ctrl-a / Ctrl-x | Increment / decrement the number under the cursor. |
gv | Reselect whatever you had selected last. |
gU / gu with a motion | Upper-case / lower-case the text a motion covers, as in gUiw. |
:earlier 10m | Travel back to how the file looked ten minutes ago. Vim keeps an undo tree, not a stack. |
:help <topic> | Vim's own manual, which is genuinely one of the best pieces of software documentation there is. |
Worth knowing
- Vim commands compose: an operator (d, c, y, >) plus a motion or text object (w, $, iw, i") is a grammar, not a list to memorise. Learn d, c and y, then learn iw, i", i( and ip, and you get dozens of commands for free.
- The dot command repeats your last change, and it is what makes Vim fast. Prefer edits that are one repeatable change — ciw then Esc, then move and press . — over a keystroke-by-keystroke fix that cannot be repeated.
- To exit Vim: press Esc, then type :q! and Enter to quit without saving, or :wq to save first. If Esc seems to do nothing you may be in a terminal running inside Vim, in which case Ctrl-\ Ctrl-n gets you back to Normal mode in Neovim.
Try a snippet instead of reading about it
Reference is faster when you can run the thing. Paste a snippet into AltF Code, the free in-browser editor — real syntax highlighting for HTML, CSS, JavaScript, Python, SQL, JSON and more, with HTML, CSS and JavaScript running live in the preview. No install and no sign-up.
Open the editorVim — common questions
- How do I exit Vim?
- Press Esc to make sure you are in Normal mode, then type :q and Enter. If Vim complains about unsaved changes, use :q! to discard them or :wq to save and quit. To close every open window at once, :qa! does it.
- What is a text object and why does it matter?
- A text object describes a region rather than a movement: iw is the word under the cursor, i" is the text inside quotes, ip is a paragraph, and the a variants (aw, a") include the surrounding delimiters or whitespace. They let you edit from anywhere inside a construct instead of positioning the cursor first.
- How do I copy to the system clipboard?
- Use the + register: "+y yanks to the system clipboard and "+p pastes from it. This needs a Vim built with clipboard support — check with :echo has("clipboard"). On Linux, installing vim-gtk or vim-gtk3 usually provides it, and Neovim needs xclip, wl-clipboard or pbcopy available.
- Should I learn Vim or Neovim?
- The editing model is identical, so anything in this sheet applies to both. Neovim ships with better defaults, a built-in terminal, LSP support and Lua configuration, which makes it the easier place to start today. Vim is what you will find preinstalled on servers, which is the reason to know the core commands either way.