Vim Cheat Sheet
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
| Command | What it does |
|---|---|
vim file | Open a file (created on first save if new) |
vim +42 file | Open at line 42 |
:w | Save |
:w newname | Save a copy under another name |
:wq / :x / ZZ | Save and quit (:x and ZZ write only if changed) |
:q | Quit (fails if there are unsaved changes) |
:q! / ZQ | Quit and discard changes |
:qa! | Quit all windows without saving |
:w !sudo tee % | Save a file you opened without sudo (see Gotchas) |
:e file | Open another file |
:e! | Reload the file, throwing away changes |
:r file | Insert another file below the cursor |
:r !date | Insert the output of a shell command |
Moving around
| Command | What it does |
|---|---|
h j k l | Left, down, up, right (arrow keys also work) |
w / b / e | Next word start / previous word start / word end |
W / B | Same, but words are separated only by whitespace |
0 / ^ / $ | Line start / first non-blank character / line end |
gg / G | First line / last line |
42G or :42 | Go to line 42 |
Ctrl+d / Ctrl+u | Half a page down / up |
Ctrl+f / Ctrl+b | Full page down / up |
% | Jump to the matching bracket |
fx / tx | Jump to / just before the next x on the line (; repeats) |
{ / } | Previous / next paragraph (blank line) |
H / M / L | Top / middle / bottom of the screen |
zz | Scroll so the cursor line is centered |
Ctrl+o / Ctrl+i | Jump back / forward through the jump list |
ma then 'a | Set mark a, jump back to it |
Inserting and editing
| Command | What it does |
|---|---|
i / a | Insert before / after the cursor |
I / A | Insert at line start / append at line end |
o / O | Open a new line below / above |
x | Delete the character under the cursor |
dd | Delete (cut) the line |
dw / d$ / D | Delete word / to end of line (D = d$) |
dG / dgg | Delete to end / start of file |
cw / cc / C | Change 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 |
rx | Replace one character with x |
R | Overwrite mode |
J | Join the next line onto this one |
>> / << | Indent / unindent the line |
=G or gg=G | Re-indent to end of file / whole file |
~ | Toggle case of the character |
. | Repeat the last change |
u / Ctrl+r | Undo / redo |
Copy, paste and registers
| Command | What it does |
|---|---|
yy / 3yy | Yank (copy) the line / three lines |
yw / y$ / yiw | Yank word / to end of line / inner word |
p / P | Paste after / before the cursor |
ddp | Swap the current line with the next |
"ayy / "ap | Yank into / paste from register a |
"+y / "+p | System clipboard (needs +clipboard, e.g. vim-gtk3 on Debian/Ubuntu, gvim on Arch) |
"0p | Paste the last yank, even after deleting something |
:reg | Show all registers |
:set paste | Disable auto-indent before pasting from the terminal (rarely needed with bracketed paste in Vim 8.1+) |
Visual mode
| Command | What it does |
|---|---|
v | Select characters |
V | Select whole lines |
Ctrl+v | Block (column) selection |
y / d / c | Yank / delete / change the selection |
> / < | Indent / unindent the selection |
Ctrl+v, select, I, type, Esc | Insert the same text on many lines (e.g. comment out a block) |
gv | Reselect the last selection |
U / u | Uppercase / lowercase the selection |
:'<,'>sort | Sort the selected lines (typing : in Visual mode fills in the range) |
Search and replace
| Command | What it does |
|---|---|
/text / ?text | Search forward / backward |
n / N | Next / previous match |
* / # | Search for the word under the cursor forward / backward |
:noh | Clear search highlighting |
/\cword | Case-insensitive search for one search |
:s/old/new/ | Replace first match on the current line |
:s/old/new/g | Replace all matches on the current line |
:%s/old/new/g | Replace in the whole file |
:%s/old/new/gc | Replace with confirmation (y, n, a all, q quit) |
:10,20s/old/new/g | Only lines 10 to 20 |
:%s/\s\+$//e | Remove trailing whitespace (e = no error if none) |
:g/pattern/d | Delete all lines matching a pattern |
:v/pattern/d | Delete all lines not matching |
:vimgrep /TODO/ **/*.py then :copen | Search across files, list results in the quickfix window |
Buffers, windows and tabs
| Command | What it does |
|---|---|
:ls | List open buffers (files) |
:bn / :bp / :b 3 | Next / previous / buffer number 3 |
:bd | Close the buffer |
:sp file / :vsp file | Split horizontally / vertically |
Ctrl+w then h j k l | Move between windows |
Ctrl+w w | Cycle through windows |
Ctrl+w = | Make all windows equal size |
Ctrl+w q / :only | Close this window / close all others |
:tabnew file | Open in a new tab |
gt / gT | Next / previous tab |
vim -O a.txt b.txt | Open two files side by side |
vimdiff a.txt b.txt | Diff two files (]c / [c jump between changes) |
:term | Open a terminal inside Vim |
Macros and useful settings
| Command | What it does |
|---|---|
qa … q | Record a macro into register a |
@a / @@ | Play macro a / repeat the last macro |
10@a | Play it 10 times |
:set number / :set relativenumber | Line numbers |
:set ignorecase smartcase | Case-insensitive search unless you type a capital |
:set expandtab tabstop=4 shiftwidth=4 | Spaces instead of tabs, width 4 |
:set list | Show tabs and trailing spaces |
:set ff=unix | Convert Windows line endings on save |
:syntax on / :set hlsearch | Syntax highlighting / highlight matches |
:help topic | Built-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
:qworks. - 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
:qwith unsaved changes. Use:wqor:q!. - "E45: 'readonly' option is set" / can't write. You opened a root-owned file. In Vim,
:w !sudo tee % > /dev/nullwrites it through sudo, then:e!reloads it (this trick does not work in Neovim, which can't pass the password prompt through). Usesudoeditnext time. - Swap file warning. "Found a swap file" means Vim crashed or the file is open elsewhere. Press
rto recover, then delete the.swpfile, orqif it's open in another session (check your tmux windows). - vi is not always Vim. On minimal Debian/Ubuntu,
viisvim-tinywithout 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 pastebefore 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.