Skip to main content

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

SyntaxWhat it does
EscReturn to Normal mode from anywhere. When you are lost, press this first.
i / aInsert before the cursor / after the cursor.
I / AInsert at the first non-blank character of the line / at the end of the line.
o / OOpen a new line below / above and start inserting.
vVisual mode: select characters.
VVisual Line mode: select whole lines.
Ctrl-vVisual Block mode: select a rectangle. The way to edit a column.
RReplace mode: overwrite characters as you type.
:Command-line mode, for :w, :q, :s and everything else.

Moving around

SyntaxWhat it does
h j k lLeft, down, up, right.
w / bForward to the next word start / back to the previous one.
e / geEnd of this word / end of the previous word.
0 / ^ / $Start of the line / first non-blank character / end of the line.
gg / GTop of the file / bottom of the file.
42G or :42Jump straight to line 42.
{ / }Previous / next paragraph, meaning the next blank line.
Ctrl-d / Ctrl-uHalf a screen down / up.
Ctrl-f / Ctrl-bA full screen forward / back.
f x / F xJump to the next / previous x on this line.
t x / T xJump 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-iBack / forward through your jump history, across files.

Editing and text objects

SyntaxWhat it does
x / XDelete the character under / before the cursor.
ddDelete the current line.
3ddDelete three lines. Almost every command takes a count like this.
dw / d$ / d0Delete to the next word / to the end of the line / to the start of the line.
dG / dggDelete to the end / to the start of the file.
cw / ccChange a word / the whole line, dropping you into Insert mode.
ciwChange 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 xReplace one character with x without leaving Normal mode.
JJoin this line with the next one.
>> / <<Indent / outdent the current line.
.Repeat the last change. The single most valuable key in Vim.
u / Ctrl-rUndo / redo.

Copy and paste

SyntaxWhat it does
yyYank (copy) the current line.
3yyYank three lines.
yw / y$Yank a word / to the end of the line.
p / PPut after / before the cursor.
dd then pCut a line and paste it elsewhere — how you move a line.
"ayy then "apYank into the named register a, then paste from it.
"+y / "+pYank to / paste from the system clipboard, on a build with clipboard support.
:regShow every register and what is currently in it.

Search and replace

SyntaxWhat it does
/textSearch forwards for text.
?textSearch backwards.
n / NNext / previous match.
* / #Search for the word under the cursor, forwards / backwards.
:%s/old/new/gReplace every occurrence in the file.
:%s/old/new/gcThe same, but confirm each replacement.
:s/old/new/gReplace only on the current line.
:10,20s/old/new/gReplace within a range of lines.
:%s/\<word\>/new/gReplace whole words only.
:nohClear the leftover search highlighting.
:g/pattern/dDelete every line that matches a pattern.
:v/pattern/dDelete every line that does not match.

Files, buffers, splits and tabs

SyntaxWhat it does
:w / :w fileSave / save as.
:q / :q!Quit / quit discarding unsaved changes.
:wq or :x or ZZSave and quit.
:qa!Quit every window, discarding changes.
:e fileOpen another file.
:e!Reload the current file from disk, throwing away your changes.
:lsList the open buffers.
:bn / :bp / :b3Next buffer / previous buffer / buffer number three.
:sp file / :vsp fileOpen a file in a horizontal / vertical split.
Ctrl-w w / Ctrl-w h j k lCycle through windows / move to the window in that direction.
Ctrl-w q / Ctrl-w oClose this window / close every window except this one.
:tabnew file, gt, gTOpen a tab, go to the next tab, go to the previous tab.

Worth knowing

SyntaxWhat it does
:set number / :set relativenumberShow absolute line numbers / distances from the cursor, which makes counts easy.
:set ignorecase smartcaseSearch case-insensitively unless you type a capital letter.
:set pasteStop auto-indent mangling pasted text. Not needed in Neovim, which handles bracketed paste.
:set spellTurn on spell checking; z= suggests corrections for the word under the cursor.
qa ... q then @aRecord a macro into register a, then replay it. @@ replays the last one again.
Ctrl-a / Ctrl-xIncrement / decrement the number under the cursor.
gvReselect whatever you had selected last.
gU / gu with a motionUpper-case / lower-case the text a motion covers, as in gUiw.
:earlier 10mTravel 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 editor

Vim — 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.