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

ModeEnter withWhat it is for
NormalEsc, Ctrl-[Moving and running operators. The resting mode.
Inserti a o I A OTyping literal text.
Visualv V Ctrl-vSelecting a range, then operating on it.
SelectghLike Visual but typing replaces the selection. Rarely used.
Command-line: / ?Ex commands and searches.
ReplaceROvertype instead of insert.
Terminal:terminalA shell running inside a Vim buffer.
Operator-pendingafter d c yVim 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
ExampleReads as
dwdelete word
3dwdelete 3 words
d3wdelete 3 words (same thing)
ci"change inside quotes
yapyank a paragraph
>i{indent inside braces
d/foo + Enterdelete 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

FormMeaning
dd yy ccOperator doubled acts on the whole line
D Y CUppercase acts from the cursor to end of line
dGDelete to end of file
dggDelete to start of file

Undo, Redo, Repeat

KeyAction
uUndo
Ctrl-rRedo
UUndo all recent changes on one line
.Repeat the last change
:earlier 5mRewind the buffer to 5 minutes ago
:later 30sRoll forward 30 seconds
:undolistShow 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

CommandAction
:wWrite the file
:w nameWrite to a new name
:waWrite every changed buffer
:qQuit (refuses if unsaved)
:q!Quit and throw away changes
:wq or :xWrite and quit
:wqaWrite everything and quit
ZZWrite and quit (Normal mode)
ZQQuit 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
PrefixNamespace
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:

TopicVimNeovim
Config file~/.vimrc~/.config/nvim/init.lua or init.vim
Config languageVimscriptLua or Vimscript
Terminal:terminal (8.0+):terminal
Language serversplugin (coc.nvim, ALE)built in (vim.lsp)
Binaryvimnvim