Vim logoVimINTERMEDIATE

Vim

Vim cheat sheet with modes, motions, commands, text objects, macros, registers, and efficient text editing shortcuts with examples.

8 min read
vimeditorshortcutscommandsproductivityterminal
Loading your progress

Modes

Understanding Vim modes and how to switch between them

Vim Modes

Different modes in Vim and how to navigate between them

text
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 mode
💡 Normal mode is the default and most powerful mode
⚡ Press Esc from any mode to return to Normal mode
📌 Use i for insert, v for visual selection, : for commands
🟢 Start with i and Esc until comfortable with other modes
modesbasics

Movement

Navigate efficiently through text

Basic Movement

Fundamental cursor movement commands

text
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 5
💡 Use numbers before commands: 5j moves down 5 lines
⚡ w/b for small words, W/B for WORDS (faster navigation)
📌 hjkl keeps your hands on home row
🟢 Master hjkl before using arrow keys
movementnavigation

Efficient navigation commands for larger jumps

text
{ - 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 screen
💡 Use f/F/t/T for precise line navigation
⚡ % is perfect for navigating code with brackets
📌 */# instantly searches for current word
🟢 Ctrl+d/u for comfortable scrolling
movementnavigationadvanced

Editing

Commands for editing and manipulating text

Basic Editing

Essential editing commands

text
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 command
💡 d is delete, c is change (delete + insert), y is yank (copy)
⚡ Use . to repeat the last edit command
📌 dd deletes line, yy copies line, p pastes
🟢 Remember: d cuts, y copies, p pastes
editingdeletecopy

Powerful text manipulation with operators and text objects

text
# 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 paragraph
💡 Combine operators (d/c/y/v) with text objects for power
⚡ "i" means inner (excludes delimiters), "a" means around
📌 ciw changes word from anywhere inside it
🟢 Master diw, ciw, and yiw first
editingtext-objectsadvanced

Visual Mode

Selecting and manipulating text visually

Commands for visual mode selection and manipulation

text
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 selection
💡 Visual block mode (Ctrl+v) for column editing
⚡ Use V for line selection, v for precise selection
📌 o jumps to other end of selection for adjustments
🟢 Press Esc to exit visual mode
visualselection

Search & Replace

Finding and replacing text

Search Commands

Search and navigation within files

text
/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 typing
💡 Use * to quickly search for word under cursor
⚡ Add \c for case-insensitive searches
📌 n/N to navigate through search results
🟢 Press / and start typing to search
searchfind

Find & Replace

Substitution and replacement commands

text
: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 only
💡 Use %s for whole file, s for current line
⚡ Add c flag for interactive replacement
📌 Visual select then :s for selection only
🟢 Remember :%s/old/new/g for global replace
searchreplacesubstitution

Files & Buffers

Managing files, buffers, and windows

File Operations

Opening, saving, and managing files

text
: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/number
💡 Use :w to save frequently
⚡ :x is shorthand for :wq
📌 :q! forces quit without saving
🟢 Remember :w (save) and :q (quit)
filesbuffers

Windows & Tabs

Managing multiple windows and tabs

text
: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 tabs
💡 Use splits for comparing files side by side
⚡ Ctrl+w then hjkl to navigate windows
📌 :vsp for vertical split is most common
🟢 gt/gT for tab navigation
windowstabssplits

Macros & Registers

Recording and using macros, working with registers

Macros

Recording and executing repetitive tasks

text
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 lines
💡 Use any letter a-z as macro register
⚡ @@ repeats the last executed macro
📌 Record once, replay many times
🟢 Start simple: record, test, then bulk apply
macrosautomation

Registers

Using registers for advanced copy/paste

text
"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 pattern
💡 Use named registers (a-z) to store multiple yanks
⚡ "+ accesses system clipboard
📌 "_ for true deletion (no register storage)
🟢 Default operations use the unnamed register ""
registersclipboard

Settings & Customization

Common Vim settings and configuration

Common Settings

Useful Vim settings and options

text
: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 scheme
💡 Add settings to ~/.vimrc for persistence
⚡ :set paste before pasting formatted code
📌 number + relativenumber for hybrid line numbers
🟢 Start with :set number and :syntax on
settingsconfiguration

Tips & Tricks

Productivity tips and useful combinations

Time-saving tips and powerful combinations

text
# 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
💡 ci + text object is incredibly powerful
⚡ Use marks for quick navigation in large files
📌 gg=G auto-formats entire file
🟢 Master ciw and ci" for quick edits
tipsproductivityadvanced