Vim
Vim cheat sheet with modes, motions, commands, text objects, macros, registers, and efficient text editing shortcuts with examples.
Setup
Install Vim, inspect the compiled feature set, and open a file.
Install Vim with a package manager and launch the editor.
# macOS
brew install vim
# Ubuntu or Debian
sudo apt update
sudo apt install vimModes
Understanding Vim modes and how to switch between them
Different modes in Vim and how to navigate between them
i - Enter Insert mode (before cursor)
I - Enter Insert mode (beginning of line)
a - Enter Insert mode (after cursor)
A - Enter Insert mode (end of line)
o - Open new line below and enter Insert mode
O - Open new line above and enter Insert mode
v - Enter Visual mode
V - Enter Visual Line mode
Ctrl+v - Enter Visual Block mode
: - Enter Command mode
Esc - Return to Normal mode
R - Enter Replace modeMovement
Navigate efficiently through text
Fundamental cursor movement commands
h - Move left
j - Move down
k - Move up
l - Move right
w - Move forward one word
b - Move backward one word
e - Move to end of word
W - Move forward one WORD (space-delimited)
B - Move backward one WORD
E - Move to end of WORD
0 - Move to beginning of line
^ - Move to first non-blank character
$ - Move to end of line
gg - Go to first line of file
G - Go to last line of file
5G or :5 - Go to line 5Efficient navigation commands for larger jumps
{ - Jump to previous paragraph
} - Jump to next paragraph
( - Jump to previous sentence
) - Jump to next sentence
% - Jump to matching bracket/parenthesis
* - Search forward for word under cursor
# - Search backward for word under cursor
n - Next search result
N - Previous search result
f<char> - Jump to next <char> on line
F<char> - Jump to previous <char> on line
t<char> - Jump before next <char> on line
T<char> - Jump after previous <char> on line
; - Repeat last f/F/t/T movement
, - Repeat last f/F/t/T movement (opposite direction)
Ctrl+f - Page down
Ctrl+b - Page up
Ctrl+d - Half page down
Ctrl+u - Half page up
zz - Center cursor on screenEditing
Commands for editing and manipulating text
Essential editing commands
x - Delete character under cursor
X - Delete character before cursor
dw - Delete word
dd - Delete line
D - Delete from cursor to end of line
cc - Change (delete and enter insert mode) entire line
C - Change from cursor to end of line
cw - Change word
s - Substitute character (delete and enter insert)
S - Substitute line
yy - Yank (copy) line
yw - Yank word
p - Paste after cursor
P - Paste before cursor
u - Undo
Ctrl+r - Redo
. - Repeat last commandPowerful text manipulation with operators and text objects
# Operators
d - Delete
c - Change (delete and enter insert)
y - Yank (copy)
v - Visual select
= - Indent
# Text Objects
iw - Inner word
aw - Around word (includes space)
is - Inner sentence
as - Around sentence
ip - Inner paragraph
ap - Around paragraph
i" - Inner quotes
a" - Around quotes (includes quotes)
i( or ib - Inner parentheses
a( or ab - Around parentheses
i{ or iB - Inner braces
a{ or aB - Around braces
it - Inner tag (HTML/XML)
at - Around tag
# Examples
diw - Delete inner word
ciw - Change inner word
yi( - Yank inside parentheses
da" - Delete around quotes
vip - Select inner paragraph
=ap - Indent around paragraphVisual Mode
Selecting and manipulating text visually
Commands for visual mode selection and manipulation
v - Start visual mode (character)
V - Start visual line mode
Ctrl+v - Start visual block mode
# In Visual Mode:
o - Move to opposite end of selection
O - Move to opposite corner (block mode)
aw - Select a word
ab - Select a block with ()
aB - Select a block with {}
ib - Select inner block with ()
iB - Select inner block with {}
ap - Select a paragraph
> - Indent selection
< - Unindent selection
= - Auto-indent selection
~ - Toggle case
u - Make lowercase
U - Make uppercase
d - Delete selection
c - Change selection
y - Yank selection
: - Enter command for selectionSearch & Replace
Finding and replacing text
Search and navigation within files
/pattern - Search forward for pattern
?pattern - Search backward for pattern
n - Next search result
N - Previous search result
* - Search forward for word under cursor
# - Search backward for word under cursor
g* - Search forward for partial word
g# - Search backward for partial word
/\cpattern - Case insensitive search
/\Cpattern - Case sensitive search
/\<word\> - Search for exact word
:noh or :nohlsearch - Clear search highlighting
:set hlsearch - Enable search highlighting
:set incsearch - Show matches while typingSubstitution and replacement commands
:s/old/new/ - Replace first on current line
:s/old/new/g - Replace all on current line
:s/old/new/gc - Replace all on line with confirmations
:%s/old/new/g - Replace all in file
:%s/old/new/gc - Replace all in file with confirmations
:5,10s/old/new/g - Replace all between lines 5-10
:'<,'>s/old/new/g - Replace all in visual selection
# Flags:
g - Global (all occurrences)
c - Confirm each replacement
i - Case insensitive
I - Case sensitive
# Special patterns:
:%s/^/text/ - Add text to beginning of all lines
:%s/$/text/ - Add text to end of all lines
:%s/\s\+$// - Remove trailing whitespace
:%s/\<word\>/new/g - Replace whole word onlyFiles & Buffers
Managing files, buffers, and windows
Opening, saving, and managing files
:e filename - Edit/open file
:w - Save current file
:w filename - Save as filename
:wq or :x - Save and quit
:q - Quit
:q! - Quit without saving
:qa - Quit all windows
:wqa - Save and quit all
:r filename - Read file into current buffer
:r !command - Read command output into buffer
:bn - Next buffer
:bp - Previous buffer
:bd - Delete buffer (close file)
:ls or :buffers - List all buffers
:b# - Switch to alternate buffer
:b name - Switch to buffer by name/numberManaging multiple windows and tabs
:sp or :split - Split window horizontally
:vsp or :vsplit - Split window vertically
:sp filename - Open file in horizontal split
:vsp filename - Open file in vertical split
Ctrl+w s - Split horizontally
Ctrl+w v - Split vertically
Ctrl+w w - Switch windows
Ctrl+w h/j/k/l - Navigate to window
Ctrl+w H/J/K/L - Move window
Ctrl+w = - Equal size windows
Ctrl+w _ - Maximize height
Ctrl+w | - Maximize width
Ctrl+w + - Increase height
Ctrl+w - - Decrease height
Ctrl+w > - Increase width
Ctrl+w < - Decrease width
Ctrl+w c - Close window
Ctrl+w o - Close all other windows
:tabnew - New tab
:tabnew filename - Open file in new tab
gt - Go to next tab
gT - Go to previous tab
:tabclose - Close tab
:tabs - List tabsMacros & Registers
Recording and using macros, working with registers
Recording and executing repetitive tasks
qa - Start recording macro in register a
q - Stop recording
@a - Execute macro from register a
@@ - Execute last macro again
5@a - Execute macro a 5 times
# Example: Add semicolon to end of lines
qa - Start recording to register a
$ - Go to end of line
a; - Append semicolon
<Esc> - Back to normal mode
j - Move to next line
q - Stop recording
@a - Execute on next line
10@a - Execute on next 10 linesUsing registers for advanced copy/paste
"ay - Yank into register a
"ap - Paste from register a
"Ay - Append to register a
"+y - Yank to system clipboard
"+p - Paste from system clipboard
"*y - Yank to selection clipboard
"*p - Paste from selection clipboard
:reg - Show all registers
:reg a - Show contents of register a
"_ - Black hole register (delete without saving)
# Special registers:
"" - Unnamed register (default)
"0 - Yank register (last yank)
"1-"9 - Delete registers (last 9 deletions)
". - Last inserted text
"% - Current filename
": - Last command
"/ - Last search patternSettings & Customization
Common Vim settings and configuration
Useful Vim settings and options
:set number or :set nu - Show line numbers
:set nonumber or :set nonu - Hide line numbers
:set relativenumber or :set rnu - Relative line numbers
:set wrap - Enable line wrapping
:set nowrap - Disable line wrapping
:set ignorecase or :set ic - Case insensitive search
:set smartcase - Smart case search
:set hlsearch - Highlight search results
:set incsearch - Incremental search
:set expandtab - Use spaces instead of tabs
:set tabstop=4 - Set tab width
:set shiftwidth=4 - Set indent width
:set autoindent - Auto indent new lines
:set paste - Paste mode (preserves formatting)
:set nopaste - Exit paste mode
:syntax on - Enable syntax highlighting
:set background=dark - Dark background
:colorscheme name - Change color schemeTips & Tricks
Productivity tips and useful combinations
Time-saving tips and powerful combinations
# Quick edits
ci" - Change inside quotes
ci( - Change inside parentheses
ci{ - Change inside braces
cit - Change inside HTML tag
ciw - Change word (from anywhere in word)
# Useful combinations
ggVG - Select entire file
gg=G - Auto-indent entire file
:%y+ - Copy entire file to clipboard
ggdG - Delete entire file
ZZ - Save and quit (like :wq)
ZQ - Quit without saving (like :q!)
# Marks
ma - Set mark a at cursor position
'a - Jump to line of mark a
`a - Jump to exact position of mark a
'' - Jump to last position
# Advanced
:!command - Run shell command
:r !date - Insert current date
:earlier 5m - Go back 5 minutes in time
:later 5m - Go forward 5 minutes
g; - Go to last edit location
g, - Go forward in edit location list
Ctrl+o - Jump to previous location
Ctrl+i - Jump to next location