Vim Cheatsheet
DevOps
vimtext-editorterminalmodal-editingsysadmin
Vim Cheatsheet
Vim (Vi IMproved) is a modal text editor. Every action is a keystroke — no menus, no mouse. Master the modes and you edit at the speed of thought.
Vim
Vim (Vi IMproved) is a modal text editor. Every action is a keystroke — no menus, no mouse. Master the modes and you edit at the speed of thought.
Modes
# Start Vim
vim file.txt # open or create file
vim # empty buffer
vim -R file.txt # read-only mode
view file.txt # alias for vim -R
| Mode | Enter | Exit / Switch | Purpose |
|---|---|---|---|
| Normal | Esc (always returns here) | — | Navigation, commands, operators |
| Insert | i, a, o, I, A, O | Esc | Text input |
| Visual | v, V, Ctrl+v | Esc | Text selection |
| Command-line | : | Enter or Esc | Ex commands (save, quit, search) |
| Replace | R | Esc | Overtype characters |
| Select | gh (after g) | Esc | Like Visual but typing replaces selection |
Navigation
Character & Line
h j k l # left, down, up, right
0 # beginning of line
^ # first non-blank character
$ # end of line
gg # first line of file
G # last line of file
5gg or 5G # go to line 5
Words, Sentences & Paragraphs
w # next word start
b # previous word start
e # next word end
ge # previous word end
W # next WORD (whitespace-delimited)
B # previous WORD
E # end of WORD
) # next sentence
( # previous sentence
} # next paragraph (blank-line separated)
{ # previous paragraph
% # matching bracket/paren/brace
Scrolling
Ctrl+f # page down
Ctrl+b # page up
Ctrl+d # half page down
Ctrl+u # half page up
Ctrl+e # scroll down one line
Ctrl+y # scroll up one line
H # top of screen
M # middle of screen
L # bottom of screen
zt # scroll current line to top
zz # scroll current line to center
zb # scroll current line to bottom
Searching
/pattern # search forward
?pattern # search backward
n # repeat search forward
N # repeat search backward
* # search forward for word under cursor
# # search backward for word under cursor
:noh # clear search highlight
Editing
Insert Mode Entry Points
i # insert before cursor
a # insert after cursor
I # insert at beginning of line (before first non-blank)
A # insert at end of line
o # open line below
O # open line above
s # delete character and enter insert
S # delete entire line and enter insert
Delete, Change, Yank
x # delete character under cursor
X # delete character before cursor
dd # delete entire line
dw # delete to next word start
d$ or D # delete to end of line
d0 # delete to beginning of line
dgg # delete to start of file
dG # delete to end of file
cc or S # change (delete + insert) entire line
cw # change to next word start
c$ or C # change to end of line
yy or Y # yank (copy) entire line
yw # yank word
y$ # yank to end of line
ygg # yank to start of file
yG # yank to end of file
p # paste after cursor
P # paste before cursor
Repeat & Undo
. # repeat last edit command
u # undo
Ctrl+r # redo
Indentation
>> # indent current line
<< # unindent current line
5>> # indent 5 lines
== # auto-indent current line
gg=G # auto-indent entire file
Search & Replace
# Basic substitute
:s/old/new/ # replace first occurrence on current line
:s/old/new/g # replace all on current line
:s/old/new/gc # replace all with confirmation
# Range substitute
:%s/old/new/g # replace all in entire file
:5,20s/old/new/g # replace in lines 5–20
:'<,'>s/old/new/g # replace in visual selection
# Advanced patterns
:%s/\t/ /g # replace tabs with 2 spaces
:%s/^\s\+// # remove leading whitespace
:%s/\s\+$// # remove trailing whitespace
:%s/foo\|bar/baz/g # replace foo or bar with baz
Marks
m{a-z} # set local mark (a-z)
m{A-Z} # set global mark (file-wide, uppercase)
'{a-z} # jump to local mark (line start)
'{A-Z} # jump to global mark
'' # jump back to last jump position
`{a-z} # jump to exact mark position (column)
:marks # list all marks
:delmarks a b # delete marks a and b
Registers
"a # use register 'a' for next delete/yank
"ayy # yank line into register a
"ap # paste from register a
"0p # paste from last yank register
"+p # paste from system clipboard
"+y # yank into system clipboard
"*p # paste from primary selection (X11)
:registers # show all registers
Macros
q{a-z} # start recording macro into register a
q # stop recording
@a # replay macro a
@@ # replay last used macro
5@a # replay macro a five times
:reg a # view contents of macro register a
Practical Macro Example
# Record a macro to wrap a word in quotes
qa # start recording into register a
i"<Esc> # insert opening quote, return to normal
ea # move to end of word
a"<Esc> # insert closing quote
q # stop recording
# Apply to every word on a line
# Position cursor on first word, then:
qa i"<Esc> ea a"<Esc> q # (this is the macro above)
100@a # replay 100 times (stops at end of line)
Visual Mode
v # character-wise visual mode
V # line-wise visual mode
Ctrl+v # block visual mode
o # go to other end of selection
gv # re-select last visual selection
Visual Mode Operations
# After selecting text in visual mode:
d # delete selection
c # change selection
y # yank (copy) selection
> # indent selection
< # unindent selection
~ # toggle case of selection
u # lowercase selection
U # uppercase selection
:sort # sort selected lines
:!fmt # reformat paragraph via fmt
Visual Block Mode
Ctrl+v # enter block visual mode
I # insert at left edge of block (applies to all lines)
A # append at right edge of block
$ # extend block to end of each line
Practical Block Mode Example
# Comment out multiple lines with //
1. Move to first line
2. Ctrl+v to enter block mode
3. jj to select 3 lines down
4. I to insert at the block's left edge
5. Type // then Esc
6. The // is inserted on all selected lines
Buffers, Windows & Tabs
Buffers
:ls or :buffers # list all buffers
:bnext or :bn # next buffer
:bprev or :bp # previous buffer
:bfirst # first buffer
:blast # last buffer
:b 3 # go to buffer 3
:b file.txt # go to buffer by name (partial match)
:bd # close current buffer
:bd 3 # close buffer 3
:bw # wipe buffer (close + delete)
:e file2.txt # open file in new buffer
:split file2.txt # open file in horizontal split
:vsp file2.txt # open file in vertical split
Windows (Splits)
:split or :sp # horizontal split
:vsplit or :vsp # vertical split
Ctrl+w s # horizontal split
Ctrl+w v # vertical split
Ctrl+w h # move to left window
Ctrl+w j # move to below window
Ctrl+w k # move to above window
Ctrl+w l # move to right window
Ctrl+w w # cycle through windows
Ctrl+w W # cycle backwards
Ctrl+w = # equalize window sizes
Ctrl+w _ # maximize current window
Ctrl+w | # maximize current window (vertical)
Ctrl+w q # close current window
Ctrl+w o # close all other windows
Tabs
:tabnew # new tab
:tabedit file.txt # open file in new tab
:tabclose or :tc # close current tab
:tabnext or :tn # next tab
:tabprev or :tp # previous tab
:tabfirst or :tf # first tab
:tablast or :tl # last tab
:tabs # list all tabs
gt # next tab
gT # previous tab
5gt # go to tab 5
File Operations
:w # save
:w! # force save (override permissions)
:w file.txt # save as new file
:q # quit
:q! # quit without saving
:wq or ZZ # save and quit
:x # save and quit (only if modified)
:e! # reload file from disk
:e file.txt # open another file
:r file.txt # insert contents of file below cursor
:r !command # insert command output below cursor
:saveas file.txt # save under new name
.vimrc Basics
# ~/.vimrc — essential settings
" General
set nocompatible # required for Vim features
set encoding=utf-8
set fileencoding=utf-8
set number # show line numbers
set relativenumber # relative line numbers for easy jumps
set scrolloff=8 # keep 8 lines of context when scrolling
set sidescrolloff=8
set signcolumn=yes # always show sign column
set cursorline # highlight current line
set colorcolumn=80 # highlight column 80
set wrap # wrap long lines
set linebreak # break at word boundaries
set showbreak=↪ # show wrap indicator
set breakindent # indent wrapped lines
" Indentation
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab # use spaces instead of tabs
set smartindent
set autoindent
filetype indent on # load indent rules per filetype
" Search
set hlsearch # highlight search results
set incsearch # incremental search
set ignorecase
set smartcase # case-sensitive when uppercase present
" Performance
set lazyredraw " don't redraw during macros
set updatetime=300 " faster update time
" Persistent undo
set undofile
set undodir=~/.vim/undodir
" Key mappings
let mapleader=" "
nnoremap <leader>w :w<CR>
nnoremap <leader>q :q<CR>
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
Tips
Emergency: How to Exit
```bash
If you're stuck in Vim, press Esc first, then:
:q! # quit without saving ```
Recover from a Crash
```bash vim -r file.txt # recover from swap file
After recovery:
:write to save, then delete the swap file
```
Next Steps
- Bash CLI Tools Cheat Sheet — Unix command-line utilities for power users
- Git CLI Cheat Sheet — Version control commands and workflows
- Nano Cheat Sheet — Simple terminal editor for quick edits
- Emacs Cheat Sheet — Extensible Lisp-based editor