Vim Cheatsheet

Workflow and Troubleshooting

Use this Vim reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

Vim Keys Outside Vim

The modal grammar shows up in tools you already use, so learning it once pays off repeatedly.

PlaceHow to get Vim keys
Bash / Zshset -o vi, or bindkey -v in zsh
Readline apps (psql, python)set editing-mode vi in ~/.inputrc
Less / manj k / n G gg already work
tmuxsetw -g mode-keys vi for copy mode
VS CodeThe VSCodeVim or Neovim extension
JetBrains IDEsIdeaVim
BrowsersVimium, Tridactyl
Gitcore.editor = vim plus git log pager keys
# ~/.bashrc
set -o vi
# Now Esc puts the command line in Normal mode: cw, dd, ., 0, $ all work.

# ~/.inputrc
set editing-mode vi
set show-mode-in-prompt on

A Practical Learning Order

You do not need every key. Learn in this order and you are productive quickly.

  1. Esc i :w :q hjkl. Enough to edit a config over SSH.
  2. dd yy p u Ctrl-r x. Basic surgery and undo.
  3. w b 0 $ gg G / n. Stop pressing l fifty times.
  4. Operators plus motions: dw cw d$ y}. The grammar clicks here.
  5. Text objects: ciw ci" ci( dap. This is the biggest single jump in speed.
  6. . and f/t/;. Repeatable precise edits.
  7. :%s, ranges, and :g. Bulk edits.
  8. Registers, macros, marks, Ctrl-o.
  9. Buffers, splits, quickfix.
  10. Config and plugins, last. Tooling is not a substitute for the grammar.

Habits to Build

Instead ofDo
Holding l or hw b f{char} t{char}
Holding j or k} { Ctrl-d Ctrl-u 42G relative counts
Arrow keyshjkl, or better, a real motion
x repeatedlydw d$ daw
Selecting with the mouseA text object
Retyping a similar edit. or a macro
Esc then i to fix a typoStay in Insert and use Ctrl-w
Leaving Insert mode to pasteCtrl-r "
Scrolling to find where you wereMarks and Ctrl-o

Anti-Patterns

  • Copying a 2,000-line vimrc. You will not know which line broke what. Start from twenty lines you understand and add only what you miss.
  • Installing plugins to avoid learning motions. A fuzzy finder does not help if you still navigate a file with arrow keys.
  • Using map instead of noremap. Recursive mappings break in ways that are painful to debug.
  • Disabling hjkl to "force" yourself. The goal is efficient motions, not banning single-step moves that are sometimes correct.
  • Remapping Esc, :, or u to something exotic. Every guide, answer, and colleague assumes the defaults.
  • :wq reflexively. Use :w while you work and keep the buffer open.
  • Ignoring :help. It is more accurate and more complete than most search results.

Recovering From Confusion

SituationFix
Unsure of the modePress Esc twice
Stuck in a pending operatorEsc
Terminal frozen after Ctrl-sCtrl-q
Ctrl-z by accidentfg in the shell
Ended up in Ex mode (":" prompt, E501)Type visual and press Enter
Recording a macro accidentally (recording @q)Press q
Search highlight everywhere:noh
"E37: No write since last change":w, or :q! to discard
"E325: ATTENTION" swap file warningAnother Vim has the file, or it crashed. Recover or delete the .swp.
File opened read-only:set noreadonly, or :w !sudo tee %
Cannot type, nothing happensYou are in Normal mode. Press i.
Broke the buffer badlyu until it is fine, or :e! to reload from disk

Common Recipes

" Delete every line matching a pattern
:g/pattern/d

" Keep only matching lines
:v/pattern/d

" Remove trailing whitespace everywhere
:%s/\s\+$//e

" Squeeze multiple blank lines into one
:%s/\n\{3,}/\r\r/e

" Convert tabs to spaces
:set expandtab | retab

" Convert CRLF to LF
:set ff=unix | w

" Sort and dedupe the whole file
:sort u

" Sort by a column
:sort /.*,/

" Reverse the file
:g/^/m 0

" Number every line
:%s/^/\=line('.').' '/

" Join every line into one
:%j!

" Uppercase every occurrence of a word
:%s/\<todo\>/\U&/g

" Insert a shell command's output
:r !git log --oneline -10

" Save as root when you forgot sudo
:w !sudo tee % > /dev/null

" Wrap every line in quotes and add a comma
:%s/.*/"&",/

" Increment every number in the file by 1
:%s/\d\+/\=submatch(0)+1/g

" Turn a comma list into lines
:s/,\s*/\r/g

" Diff the buffer against the file on disk
:w !diff % -

One-Page Summary

CategoryKeys
Modesi a o v V Ctrl-v : Esc
Movehjkl w b e 0 ^ $ gg G { } % f t H M L
Editd c y p x r J > < = gu gU ~
Objectsiw aw i" a" i( a( i{ it ip
Undou Ctrl-r .
Search/ ? n N * :%s///g :g//
Registers"ayy "ap "+y :reg
Macrosqaq then @a 99@a
Marksma ` a ` Ctrl-o Ctrl-i `
Files:e :w :q :ls :b Ctrl-^
Windows:sp :vs Ctrl-w hjkl Ctrl-w = Ctrl-w o
Help:help x Ctrl-] Ctrl-o :helpgrep

Where to Go Next

  • :help user-manual is a genuine book, organized as a course, and it is already on your machine.
  • vimtutor at the shell is a 30-minute hands-on introduction. Run it once even if you already know the basics.
  • :help quickref is a single-page key reference.
  • :help vim-modes, :help text-objects, and :help registers are the three sections that repay careful reading the most.