Vim Cheat Sheet

Short answer: survive Vim
i          # enter Insert mode and type text
Esc        # back to Normal mode
:w         # save
:wq        # save and quit (or ZZ)
:q!        # quit without saving
u          # undo (Ctrl+r redoes)

Vim is modal: Normal mode for commands and movement (where you start), Insert mode for typing, Visual mode for selecting, and Command-line mode after :. Most Normal-mode commands combine as operator + motion, e.g. d (delete) + w (word) = dw, and accept a count: 3dd deletes three lines. Run vimtutor for a 30-minute interactive lesson.

Many minimal installs ship only vim-tiny or vi. Install full Vim with sudo apt install vim, sudo dnf install vim-enhanced, sudo pacman -S vim or sudo zypper install vim.

Files, saving and quitting

CommandWhat it does
vim fileOpen a file (created on first save if new)
vim +42 fileOpen at line 42
:wSave
:w newnameSave a copy under another name
:wq / :x / ZZSave and quit (:x and ZZ write only if changed)
:qQuit (fails if there are unsaved changes)
:q! / ZQQuit and discard changes
:qa!Quit all windows without saving
:w !sudo tee %Save a file you opened without sudo (see Gotchas)
:e fileOpen another file
:e!Reload the file, throwing away changes
:r fileInsert another file below the cursor
:r !dateInsert the output of a shell command

Moving around

CommandWhat it does
h j k lLeft, down, up, right (arrow keys also work)
w / b / eNext word start / previous word start / word end
W / BSame, but words are separated only by whitespace
0 / ^ / $Line start / first non-blank character / line end
gg / GFirst line / last line
42G or :42Go to line 42
Ctrl+d / Ctrl+uHalf a page down / up
Ctrl+f / Ctrl+bFull page down / up
%Jump to the matching bracket
fx / txJump to / just before the next x on the line (; repeats)
{ / }Previous / next paragraph (blank line)
H / M / LTop / middle / bottom of the screen
zzScroll so the cursor line is centered
Ctrl+o / Ctrl+iJump back / forward through the jump list
ma then 'aSet mark a, jump back to it

Inserting and editing

CommandWhat it does
i / aInsert before / after the cursor
I / AInsert at line start / append at line end
o / OOpen a new line below / above
xDelete the character under the cursor
ddDelete (cut) the line
dw / d$ / DDelete word / to end of line (D = d$)
dG / dggDelete to end / start of file
cw / cc / CChange word / line / to end of line (delete and enter Insert)
ciw / ci" / ci(Change inside word / quotes / parentheses
daw / da"Delete a word / quoted string including surroundings
rxReplace one character with x
ROverwrite mode
JJoin the next line onto this one
>> / <<Indent / unindent the line
=G or gg=GRe-indent to end of file / whole file
~Toggle case of the character
.Repeat the last change
u / Ctrl+rUndo / redo

Copy, paste and registers

CommandWhat it does
yy / 3yyYank (copy) the line / three lines
yw / y$ / yiwYank word / to end of line / inner word
p / PPaste after / before the cursor
ddpSwap the current line with the next
"ayy / "apYank into / paste from register a
"+y / "+pSystem clipboard (needs +clipboard, e.g. vim-gtk3 on Debian/Ubuntu, gvim on Arch)
"0pPaste the last yank, even after deleting something
:regShow all registers
:set pasteDisable auto-indent before pasting from the terminal (rarely needed with bracketed paste in Vim 8.1+)

Visual mode

CommandWhat it does
vSelect characters
VSelect whole lines
Ctrl+vBlock (column) selection
y / d / cYank / delete / change the selection
> / <Indent / unindent the selection
Ctrl+v, select, I, type, EscInsert the same text on many lines (e.g. comment out a block)
gvReselect the last selection
U / uUppercase / lowercase the selection
:'<,'>sortSort the selected lines (typing : in Visual mode fills in the range)

Search and replace

CommandWhat it does
/text / ?textSearch forward / backward
n / NNext / previous match
* / #Search for the word under the cursor forward / backward
:nohClear search highlighting
/\cwordCase-insensitive search for one search
:s/old/new/Replace first match on the current line
:s/old/new/gReplace all matches on the current line
:%s/old/new/gReplace in the whole file
:%s/old/new/gcReplace with confirmation (y, n, a all, q quit)
:10,20s/old/new/gOnly lines 10 to 20
:%s/\s\+$//eRemove trailing whitespace (e = no error if none)
:g/pattern/dDelete all lines matching a pattern
:v/pattern/dDelete all lines not matching
:vimgrep /TODO/ **/*.py then :copenSearch across files, list results in the quickfix window

Buffers, windows and tabs

CommandWhat it does
:lsList open buffers (files)
:bn / :bp / :b 3Next / previous / buffer number 3
:bdClose the buffer
:sp file / :vsp fileSplit horizontally / vertically
Ctrl+w then h j k lMove between windows
Ctrl+w wCycle through windows
Ctrl+w =Make all windows equal size
Ctrl+w q / :onlyClose this window / close all others
:tabnew fileOpen in a new tab
gt / gTNext / previous tab
vim -O a.txt b.txtOpen two files side by side
vimdiff a.txt b.txtDiff two files (]c / [c jump between changes)
:termOpen a terminal inside Vim

Macros and useful settings

CommandWhat it does
qa … qRecord a macro into register a
@a / @@Play macro a / repeat the last macro
10@aPlay it 10 times
:set number / :set relativenumberLine numbers
:set ignorecase smartcaseCase-insensitive search unless you type a capital
:set expandtab tabstop=4 shiftwidth=4Spaces instead of tabs, width 4
:set listShow tabs and trailing spaces
:set ff=unixConvert Windows line endings on save
:syntax on / :set hlsearchSyntax highlighting / highlight matches
:help topicBuilt-in help (:help :s, :help dw)

Common recipes

A minimal ~/.vimrc (Vim 9 also reads ~/.vim/vimrc):

set number relativenumber
set ignorecase smartcase incsearch hlsearch
set expandtab tabstop=4 shiftwidth=4 autoindent
set mouse=a
set undofile undodir=~/.vim/undo//
syntax on
filetype plugin indent on
mkdir -p ~/.vim/undo

Make Vim the default editor for sudo -e, crontab -e, git commit and systemctl edit:

echo 'export EDITOR=vim VISUAL=vim' >> ~/.bashrc
sudo update-alternatives --config editor    # Debian/Ubuntu system default

Comment out lines 10 to 20 in a shell script, then uncomment them:

:10,20s/^/# /
:10,20s/^# //

Run the same edit on many files from the command line:

vim -c 'bufdo %s/foo/bar/ge | update' -c 'qa' *.txt

Edit a root-owned file safely as your user (opens a copy in your own editor):

sudoedit /etc/hosts

Delete every line containing DEBUG, then remove blank lines:

:g/DEBUG/d
:g/^\s*$/d

Gotchas

  • Stuck? Press Esc twice. You are probably in Insert or Visual mode, or half-way through a command. Esc gets you back to Normal mode where :q works.
  • Ctrl+s freezes the terminal. That's terminal flow control, not Vim. Press Ctrl+q to unfreeze, then save with :w.
  • "E37: No write since last change". You tried :q with unsaved changes. Use :wq or :q!.
  • "E45: 'readonly' option is set" / can't write. You opened a root-owned file. In Vim, :w !sudo tee % > /dev/null writes it through sudo, then :e! reloads it (this trick does not work in Neovim, which can't pass the password prompt through). Use sudoedit next time.
  • Swap file warning. "Found a swap file" means Vim crashed or the file is open elsewhere. Press r to recover, then delete the .swp file, or q if it's open in another session (check your tmux windows).
  • vi is not always Vim. On minimal Debian/Ubuntu, vi is vim-tiny without syntax highlighting or many features; on some systems it's BusyBox vi or nvi. Install the full package.
  • Pasted text gets staircased. In a terminal without bracketed paste, auto-indent re-indents every line. Use :set paste before pasting, or paste with "+p.

Reference: vimhelp.org (the same as :help). New to the shell around Vim? See the Linux terminal guide.

Frequently asked questions

How do I exit Vim?

Press Esc, then type :q and Enter. Use :wq to save and quit, or :q! to quit and discard changes.

How do I search and replace in Vim?

:%s/old/new/g replaces every match in the file. Add c (:%s/old/new/gc) to confirm each one.

How do I copy and paste in Vim?

yy copies (yanks) a line, p pastes after the cursor. To use the system clipboard, use "+y and "+p; this needs a Vim built with clipboard support, such as the vim-gtk3 package on Debian/Ubuntu.

How do I undo and redo in Vim?

u undoes the last change and Ctrl+r redoes it.

Why does pressing a key in Vim not type text?

Vim starts in Normal mode, where keys are commands. Press i to enter Insert mode and type; press Esc to go back.

Spotted a mistake or a command that no longer works? Send a correction — pages are re-checked when reports come in.