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.
| Place | How to get Vim keys |
|---|---|
| Bash / Zsh | set -o vi, or bindkey -v in zsh |
| Readline apps (psql, python) | set editing-mode vi in ~/.inputrc |
| Less / man | j k / n G gg already work |
| tmux | setw -g mode-keys vi for copy mode |
| VS Code | The VSCodeVim or Neovim extension |
| JetBrains IDEs | IdeaVim |
| Browsers | Vimium, Tridactyl |
| Git | core.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.
Esci:w:qhjkl. Enough to edit a config over SSH.ddyypuCtrl-rx. Basic surgery and undo.wb0$ggG/n. Stop pressinglfifty times.- Operators plus motions:
dwcwd$y}. The grammar clicks here. - Text objects:
ciwci"ci(dap. This is the biggest single jump in speed. .andf/t/;. Repeatable precise edits.:%s, ranges, and:g. Bulk edits.- Registers, macros, marks,
Ctrl-o. - Buffers, splits, quickfix.
- Config and plugins, last. Tooling is not a substitute for the grammar.
Habits to Build
| Instead of | Do |
|---|---|
Holding l or h | w b f{char} t{char} |
Holding j or k | } { Ctrl-d Ctrl-u 42G relative counts |
| Arrow keys | hjkl, or better, a real motion |
x repeatedly | dw d$ daw |
| Selecting with the mouse | A text object |
| Retyping a similar edit | . or a macro |
Esc then i to fix a typo | Stay in Insert and use Ctrl-w |
| Leaving Insert mode to paste | Ctrl-r " |
| Scrolling to find where you were | Marks 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
mapinstead ofnoremap. Recursive mappings break in ways that are painful to debug. - Disabling
hjklto "force" yourself. The goal is efficient motions, not banning single-step moves that are sometimes correct. - Remapping
Esc,:, oruto something exotic. Every guide, answer, and colleague assumes the defaults. :wqreflexively. Use:wwhile you work and keep the buffer open.- Ignoring
:help. It is more accurate and more complete than most search results.
Recovering From Confusion
| Situation | Fix |
|---|---|
| Unsure of the mode | Press Esc twice |
| Stuck in a pending operator | Esc |
Terminal frozen after Ctrl-s | Ctrl-q |
Ctrl-z by accident | fg 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 warning | Another 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 happens | You are in Normal mode. Press i. |
| Broke the buffer badly | u 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
| Category | Keys |
|---|---|
| Modes | i a o v V Ctrl-v : Esc |
| Move | hjkl w b e 0 ^ $ gg G { } % f t H M L |
| Edit | d c y p x r J > < = gu gU ~ |
| Objects | iw aw i" a" i( a( i{ it ip |
| Undo | u Ctrl-r . |
| Search | / ? n N * :%s///g :g// |
| Registers | "ayy "ap "+y :reg |
| Macros | qa … q then @a 99@a |
| Marks | ma ` 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-manualis a genuine book, organized as a course, and it is already on your machine.vimtutorat the shell is a 30-minute hands-on introduction. Run it once even if you already know the basics.:help quickrefis a single-page key reference.:help vim-modes,:help text-objects, and:help registersare the three sections that repay careful reading the most.