Vim Cheatsheet
Basics
Use this Vim reference while you build software engineering projects, review code, or refresh the syntax you reach for most.
What Vim Is
Vim is a modal text editor. Keys mean different things depending on the mode you are in, so most editing happens without modifier chords. The core loop is: stay in Normal mode, move with motions, apply an operator, drop into Insert mode only to type.
Vim ships on nearly every Unix machine, which is why it is the editor you fall back to over SSH, inside a container, or in a git commit buffer.
vim file.txt # Open a file vim +42 file.txt # Open at line 42 vim +/pattern file.txt # Open at the first match of pattern vim -R file.txt # Read-only vim -d a.txt b.txt # Diff mode (vimdiff) vim -u NONE file.txt # No config, no plugins (clean repro) vim -p a.txt b.txt # Open each file in its own tab vim -O a.txt b.txt # Open side by side (vertical splits)
The Modes
| Mode | Enter with | What it is for |
|---|---|---|
| Normal | Esc, Ctrl-[ | Moving and running operators. The resting mode. |
| Insert | i a o I A O | Typing literal text. |
| Visual | v V Ctrl-v | Selecting a range, then operating on it. |
| Select | gh | Like Visual but typing replaces the selection. Rarely used. |
| Command-line | : / ? | Ex commands and searches. |
| Replace | R | Overtype instead of insert. |
| Terminal | :terminal | A shell running inside a Vim buffer. |
| Operator-pending | after d c y … | Vim is waiting for the motion or text object. |
Esc always returns to Normal mode. If you are unsure where you are, press it twice.
The Grammar
Almost every Normal-mode command is built from the same three pieces.
[count] operator [count] motion-or-text-object
| Example | Reads as |
|---|---|
dw | delete word |
3dw | delete 3 words |
d3w | delete 3 words (same thing) |
ci" | change inside quotes |
yap | yank a paragraph |
>i{ | indent inside braces |
d/foo + Enter | delete up to the next "foo" |
Learn the operators and the motions separately, and every combination of them comes free. That composability is the whole reason to learn Vim rather than memorize a key list.
Doubling and Uppercase
| Form | Meaning |
|---|---|
dd yy cc | Operator doubled acts on the whole line |
D Y C | Uppercase acts from the cursor to end of line |
dG | Delete to end of file |
dgg | Delete to start of file |
Undo, Redo, Repeat
| Key | Action |
|---|---|
u | Undo |
Ctrl-r | Redo |
U | Undo all recent changes on one line |
. | Repeat the last change |
:earlier 5m | Rewind the buffer to 5 minutes ago |
:later 30s | Roll forward 30 seconds |
:undolist | Show the undo tree branches |
The dot command is the highest-leverage key in Vim. Make an edit precise enough to repeat, then press . to apply it again at the next spot.
" Change every 'foo' to 'bar', reviewing each one /foo<Enter> " find the first cwbar<Esc> " change the word n. " next match, repeat the change n. " and again
Saving and Quitting
| Command | Action |
|---|---|
:w | Write the file |
:w name | Write to a new name |
:wa | Write every changed buffer |
:q | Quit (refuses if unsaved) |
:q! | Quit and throw away changes |
:wq or :x | Write and quit |
:wqa | Write everything and quit |
ZZ | Write and quit (Normal mode) |
ZQ | Quit without writing |
:w !sudo tee % | Write a root-owned file you opened as a user |
Getting Help
Vim's manual is the reference, and it is searchable from inside the editor.
:help " The main help index :help dd " Help for a Normal-mode command :help i_CTRL-W " Insert-mode mapping (note the i_ prefix) :help :set " Help for an Ex command :help 'number' " Help for an option (quotes) :help text-objects " Help for a topic :helpgrep register " Search all help files Ctrl-] " Follow the tag under the cursor Ctrl-o " Jump back
| Prefix | Namespace |
|---|---|
i_ | Insert mode |
v_ | Visual mode |
c_ | Command-line mode |
: | Ex command |
'…' | Option |
Vim vs Neovim
Neovim is a fork of Vim with a rewritten core, built-in LSP, Lua configuration, and an async job API. Everything in this reference works in both. The differences that matter day to day:
| Topic | Vim | Neovim |
|---|---|---|
| Config file | ~/.vimrc | ~/.config/nvim/init.lua or init.vim |
| Config language | Vimscript | Lua or Vimscript |
| Terminal | :terminal (8.0+) | :terminal |
| Language servers | plugin (coc.nvim, ALE) | built in (vim.lsp) |
| Binary | vim | nvim |