Vim Command Cheatsheet
Vim is synonymous with efficient text editing. This cheatsheet covers the most commonly used Vim commands — from basic operations to macro recording — each with practical examples. Whether you're new to Vim or looking to level up, this guide has you covered. Start with basic operations and cursor movement, then gradually explore search, windows, and advanced features.
Quick Reference
| Task | Key / Command |
| Start editing | i (insert before cursor) |
| Save file | :w |
| Save and quit | :wq or ZZ |
| Copy a line | yy |
| Delete a line | dd |
| Paste | p (after) / P (before) |
| Undo | u |
| Search | /pattern |
| Replace all | :%s/old/new/g |
| Split window | :vs (vertical) / :sp (horizontal) |
Basic Operations
``bash
# Mode switching
i # Insert before cursor
a # Insert after cursor
o # Open new line below and insert
I # Insert at line beginning
A # Insert at line end
O # Open new line above and insert
Esc / Ctrl+[ # Return to normal mode
# File operations
:e file.txt # Open a file
:w # Save
:q # Quit
:wq / ZZ # Save and quit
:q! # Force quit (discard changes)
:e! # Reload file (undo all changes)
:help topic # Open help
`
Cursor Movement
`bash
# Basic movement
h # Left
j # Down
k # Up
l # Right
5j # Move down 5 lines
# Word movement w # Forward to word start b # Backward to word start e # Forward to word end ge # Backward to word end 3w # Forward 3 words
# Line movement 0 # First column ^ # First non-blank character $ # End of line g_ # Last non-blank character
# File navigation gg # First line G # Last line 42G / :42 # Jump to line 42 Ctrl+d # Scroll down half page Ctrl+u # Scroll up half page Ctrl+f # Scroll down full page Ctrl+b # Scroll up full page
# Code blocks % # Matching bracket ( / ) # Previous/next sentence { / } # Previous/next paragraph
# Screen positioning
zz # Center cursor on screen
zt # Top of screen
zb # Bottom of screen
`
Editing
`bash
# Delete
x # Delete character under cursor
X # Delete character before cursor
3x # Delete 3 characters
dd # Delete current line
5dd # Delete 5 lines
D # Delete to end of line
dw # Delete to word end
diw # Delete inner word
daw # Delete a word (including space)
di( # Delete inside parentheses
da" # Delete around quotes
# Yank (Copy) yy # Yank current line 5yy # Yank 5 lines yiw # Yank inner word y$ # Yank to end of line
# Paste p # Paste after cursor P # Paste before cursor
# Change r # Replace character R # Enter replace mode ciw # Change inner word ci( # Change inside parentheses c$ # Change to end of line cc # Change entire line
# Undo & Repeat
u # Undo
Ctrl+r # Redo
. # Repeat last change
`
Visual Mode
`bash
# Enter visual mode
v # Character-wise visual
V # Line-wise visual
Ctrl+v # Block visual (column editing)
# Visual operations (select then press) d # Delete selection y # Yank (copy) selection > # Indent right < # Indent left ~ # Toggle case u # Lowercase U # Uppercase : # Execute command on selection
# Column editing trick
Ctrl+v # Enter block select
jjj # Select multiple lines downwards
I # Insert at column start
text # Type the text to insert
Esc # Apply to all selected lines
`
Search & Replace
`bash
# Search
/pattern # Search forward
?pattern # Search backward
n # Next match
N # Previous match
* # Search word under cursor (forward)
# # Search word under cursor (backward)
# Highlight control :nohlsearch # Clear search highlighting set hlsearch # Enable highlight set nohlsearch # Disable highlight
# Replace
:s/old/new/ # Replace first match on current line
:s/old/new/g # Replace all on current line
:%s/old/new/g # Replace in whole file
:%s/old/new/gc # Replace with confirmation
:5,20s/old/new/g # Replace in lines 5-20
:%s/old/new/gn # Count matches (no actual replace)
`
Windows & Tabs
`bash
# Window management
:sp [file] # Split horizontally
:vs [file] # Split vertically
Ctrl+w h # Go to left window
Ctrl+w j # Go to window below
Ctrl+w k # Go to window above
Ctrl+w l # Go to right window
Ctrl+w w # Cycle through windows
Ctrl+w = # Equalize window sizes
Ctrl+w _ # Maximize window height
Ctrl+w | # Maximize window width
Ctrl+w + # Increase height
Ctrl+w - # Decrease height
:q # Close current window
:only # Close all but current
# Tab pages
:tabnew [file] # Open in new tab
gt # Next tab
gT # Previous tab
Ngt # Go to tab N
:tabclose # Close current tab
:tabonly # Close other tabs
:tabmove +1 # Move tab right
`
Advanced Techniques
`bash
# Macros
qa # Start recording macro 'a'
... # Perform your actions
q # Stop recording
@a # Play macro 'a'
@@ # Repeat last macro
5@a # Play macro 'a' 5 times
# Registers "ayy # Yank current line to register 'a' "ap # Paste from register 'a' :reg # View all registers "+y # Yank to system clipboard "+p # Paste from system clipboard
# Marks ma # Set mark 'a' at cursor a # Jump to mark 'a' 'a # Jump to line of mark 'a' :marks # List all marks
# Autocomplete (insert mode) Ctrl+p # Previous completion Ctrl+n # Next completion Ctrl+x Ctrl+l # Complete whole line Ctrl+x Ctrl+f # Complete filename Ctrl+x Ctrl+] # Complete tag
# Run shell commands from Vim
:!ls # Run 'ls' command
:!python % # Run current Python file
:read !date # Insert command output
:r !curl -s URL # Insert curl result
``
Vim Configuration
`vim
" ~/.vimrc common settings
" Interface set number " Show line numbers set relativenumber " Show relative line numbers set cursorline " Highlight current line set showmode " Show current mode set showcmd " Show partial commands
" Indentation set tabstop=4 " Tab width: 4 set shiftwidth=4 " Indent width: 4 set expandtab " Use spaces instead of tabs set autoindent " Auto indent set smartindent " Smart indent
" Search set hlsearch " Highlight search results set incsearch " Incremental search set ignorecase " Ignore case set smartcase " Smart case sensitivity
" Editing set backspace=indent,eol,start " Allow backspace over anything set clipboard=unnamedplus " Use system clipboard syntax on " Enable syntax highlighting
" File type
filetype plugin indent on " Load filetype-specific plugins
`
Vim Learning Path
`
Beginner → Basic operations + cursor movement (hjkl + counts)
Daily use → Delete/yank/paste + search/replace + undo/redo
Efficiency → Text objects (diw/ci(/da") + block visual mode
Advanced → Macros + registers + multi-file editing + customization
``
Basic(9)
| Command | Level | ||
|---|---|---|---|
i / a / oEnter insert mode (before cursor / after cursor / new line) | Basic | i | |
Esc / Ctrl+[Return to normal mode | Basic | <Esc> | |
:wSave the file | Basic | :w | |
:qQuit Vim | Basic | :q | |
:wq / ZZSave and quit | Basic | :wq | |
:q!Force quit (discard changes) | Basic | :q! | |
:e <file>Open a file | Basic | :e ~/.vimrc | |
:help <topic>Open Vim built-in help | Intermediate | :help i_Ctrl-p | |
vimtutorLaunch the Vim interactive tutorial (best way for beginners) | Basic | vimtutor |
editing(7)
| Command | Level | ||
|---|---|---|---|
x / XDelete character under cursor / before cursor | Basic | 3x | |
dd / yy / p / PDelete line / yank line / paste after / paste before | Basic | dd; 5yy; p | |
diw / ciw / yiwDelete / change / yank inner word | Intermediate | diw | |
da" / ci( / ya{Delete / change / yank around quotes/brackets (including delimiters) | Intermediate | da" | |
r<char>Replace character under cursor | Basic | ra | |
. (dot)Repeat the last change | Intermediate | . | |
u / Ctrl+rUndo / redo | Basic | u |
Visual Mode(3)
| Command | Level | ||
|---|---|---|---|
v / V / Ctrl+vCharacter-wise / line-wise / block-wise visual mode | Intermediate | Ctrl+v | |
> / <Indent / unindent selected lines | Basic | > # 向右缩进 | |
~ / u / UToggle case / lowercase / uppercase | Basic | |
Search(5)
| Command | Level | ||
|---|---|---|---|
/<pattern> / ?<pattern>Search forward / search backward | Basic | /function | |
n / NJump to next / previous match | Basic | n | |
* / #Search word under cursor (forward / backward) | Basic | * # 向下搜索当前词 | |
:%s/<old>/<new>/gSearch and replace across entire file | Intermediate | :%s/foo/bar/g | |
:%s/<old>/<new>/gcSearch and replace with confirmation | Intermediate | :%s/foo/bar/gc |
Windows(5)
| Command | Level | ||
|---|---|---|---|
:sp <file> / :vs <file>Split horizontally / vertically to open a file | Intermediate | :vs ~/.vimrc | |
Ctrl+w h/j/k/lNavigate to left / down / up / right window | Intermediate | Ctrl+w l | |
Ctrl+w = / _ / |Equalize windows / maximize height / maximize width | Intermediate | Ctrl+w = | |
:tabnew / gt / gTNew tab / next tab / previous tab | Intermediate | gt | |
:tabm <n>Move the current tab page to position N | Intermediate | :tabm 0 |
Advanced(6)
| Command | Level | ||
|---|---|---|---|
q<letter> ... qStart recording a macro / stop recording | Expert | qa | |
@<letter> / @@Play a macro / repeat last macro | Expert | @a # 播放宏 a | |
"<reg>p / "<reg>yPaste / yank using a specific register | Expert | "ayy # 复制到寄存器 a | |
m<letter> / '<letter>Set a mark / jump to a mark | Intermediate | ma; 'a | |
:!<command>Execute a shell command from Vim | Intermediate | :!ls -la | |
Ctrl+p / Ctrl+nAutocomplete in insert mode (previous / next) | Intermediate | Ctrl+p |