Vim Cheat Sheet: Every Command You Need to Know

6 min read
Intermediate Vim Editor Linux Cheat Sheet

Quick Answer: vim file.txt opens a file. You start in Normal mode — press i to type (Insert mode), Esc to go back to Normal. Save and quit: :wq. Quit without saving: :q!. That's enough to survive. Everything below makes you fast.

The Three Modes

Mode What It Does Enter It Leave It
Normal Navigate, delete, copy, paste Esc Press i, :, or /
Insert Type text i, a, o, A, O Esc
Command Save, quit, search, replace : or / Enter or Esc

Rule: If you're confused, press Esc to go back to Normal mode.

Save and Quit

:w              Save
:q              Quit (fails if unsaved changes)
:wq             Save and quit
:x              Save and quit (writes only if modified, unlike :wq)
ZZ              Save and quit (Normal mode shortcut)
:q!             Quit WITHOUT saving (discard changes)
:w filename     Save as different filename
:wa             Save all open files
:qa             Quit all open files

Navigation

Basic Movement

h               Left
j               Down
k               Up
l               Right

Word Movement

w               Next word (start)
W               Next WORD (whitespace-separated)
e               End of word
b               Previous word
B               Previous WORD

Line Movement

0               Start of line
^               First non-blank character
$               End of line

Screen Movement

gg              First line of file
G               Last line of file
:42             Go to line 42
42G             Go to line 42
H               Top of screen
M               Middle of screen
L               Bottom of screen
Ctrl+u          Half page up
Ctrl+d          Half page down
Ctrl+f          Full page forward
Ctrl+b          Full page back

Search

/pattern        Search forward
?pattern        Search backward
n               Next match
N               Previous match
*               Search word under cursor (forward)
#               Search word under cursor (backward)

Editing

Enter Insert Mode

i               Insert before cursor
I               Insert at start of line
a               Append after cursor
A               Append at end of line
o               New line below
O               New line above
s               Delete char and enter insert
S               Delete line and enter insert

Delete

x               Delete character under cursor
X               Delete character before cursor (backspace)
dw              Delete word
d$  or  D       Delete to end of line
dd              Delete entire line
3dd  or  3dd    Delete 3 lines
dG              Delete to end of file
dgg             Delete to start of file
dt)             Delete until next )
di"             Delete inside quotes
da"             Delete around quotes (includes quotes)
diw             Delete inner word
dip             Delete inner paragraph

Change (Delete and Enter Insert Mode)

cw              Change word
c$  or  C       Change to end of line
cc              Change entire line
ci"             Change inside quotes
ci(             Change inside parentheses
ci{             Change inside braces
ct)             Change until )

Undo and Redo

u               Undo
Ctrl+r          Redo
.               Repeat last command

Replace

r               Replace single character (stay in Normal mode)
R               Replace mode (overwrite until Esc)
~               Toggle case of character
gUw             Uppercase word
guw             Lowercase word
gUU             Uppercase entire line
guu             Lowercase entire line

Copy and Paste

In Vim, copy is called yank and cut is delete.

yy              Yank (copy) entire line
2yy             Yank 2 lines
yw              Yank word
y$              Yank to end of line
yiw             Yank inner word

p               Paste after cursor
P               Paste before cursor

dd              Cut (delete) line — it goes to paste buffer
x               Cut character

Registers (Clipboard)

"ayy            Yank line to register 'a'
"ap             Paste from register 'a'
"Ayy            Append to register 'a'
:reg            Show all registers
"+y             Yank to system clipboard
"+p             Paste from system clipboard

Visual Mode (Selection)

v               Start character selection
V               Start line selection
Ctrl+v          Start block (column) selection

# After selecting:
d               Delete selection
y               Yank (copy) selection
c               Change (replace) selection
>               Indent
<               Unindent
=               Auto-indent
U               Uppercase
u               Lowercase

Search and Replace

:s/old/new/             Replace first on current line
:s/old/new/g            Replace all on current line
:%s/old/new/g           Replace all in entire file
:%s/old/new/gc          Replace all with confirmation
:10,20s/old/new/g       Replace in lines 10-20

Useful Patterns

:%s/\s\+$//g            Remove trailing whitespace
:%s/\t/    /g           Replace tabs with 4 spaces
:%s/\r//g               Remove Windows line endings (^M)
:%s/^/#/g               Comment all lines (prepend #)
:%s/^#//g               Uncomment all lines

Multiple Files

Buffers

:e filename             Open file in new buffer
:ls                     List open buffers
:bn                     Next buffer
:bp                     Previous buffer
:b3                     Go to buffer 3
:bd                     Close buffer

Splits

:split filename         Horizontal split
:vsplit filename        Vertical split
Ctrl+w s                Split horizontal
Ctrl+w v                Split vertical
Ctrl+w w                Switch between splits
Ctrl+w h/j/k/l         Navigate splits
Ctrl+w =                Equal size splits
Ctrl+w q                Close split
Ctrl+w o                Close all other splits

Tabs

:tabnew filename        Open in new tab
:tabn  or  gt           Next tab
:tabp  or  gT           Previous tab
:tabclose               Close tab
:tabs                   List tabs

Macros

Record a sequence of commands and replay it.

qa              Start recording macro to register 'a'
(do commands)
q               Stop recording

@a              Play macro 'a'
@@              Replay last macro
10@a            Play macro 'a' 10 times

Example: Add // Comment to Lines

qa              Start recording
I//             Go to start, enter insert, type //
Esc             Back to normal
j               Move down
q               Stop recording

99@a            Apply to next 99 lines

Text Objects

Vim's power feature. Combine with d, c, y, or v:

iw              Inner word
aw              A word (includes surrounding space)
is              Inner sentence
ip              Inner paragraph
i"              Inside double quotes
i'              Inside single quotes
i(  or  ib      Inside parentheses
i{  or  iB      Inside braces
i[              Inside brackets
i<              Inside angle brackets
it              Inside HTML tags

Examples:

ci"             Change everything inside "..."
da(             Delete everything including (...)
yi{             Yank everything inside {...}
vit             Select inside HTML tag

Marks

ma              Set mark 'a' at cursor position
'a              Jump to mark 'a' (line)
`a              Jump to mark 'a' (exact position)
:marks          Show all marks
''              Jump to last position before jump

Configuration (~/.vimrc)

Recommended Starter Config

" Line numbers
set number
set relativenumber

" Search
set ignorecase
set smartcase
set incsearch
set hlsearch

" Indentation
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent

" Quality of life
set mouse=a
set clipboard=unnamedplus
set wrap
set linebreak
set scrolloff=8
set wildmenu
set showcmd
set showmatch
set backspace=indent,eol,start

" Performance
set lazyredraw
set ttyfast

" No swap files
set noswapfile
set nobackup

" Colors
syntax on
set background=dark
colorscheme desert

" Status line
set laststatus=2
set statusline=%f\ %m%r%h%w\ [%l/%L,\ %c]\ %p%%

Survival Kit (Minimum to Know)

If you only remember 10 things:

Command What
i Start typing
Esc Stop typing
:wq Save and quit
:q! Quit without saving
dd Delete line
u Undo
yy / p Copy line / paste
/word Search
:%s/old/new/g Replace all
:42 Go to line 42

Vim vs Nano

Vim Nano
Learning curve Steep Easy
Speed once learned Very fast Moderate
Available everywhere Yes (vi at minimum) Usually
Extensible Highly (plugins, vimrc) Limited
Use when Daily editing, servers Quick one-off edits

See Also