Vim Cheatsheet - Modes, Editing & Search Commands
The real barrier in Vim is not configuration but two layers of muscle memory: motions that let you travel precisely without scrolling, and search-and-replace patterns confident enough to run in bulk. On a headless server, editing nginx, sshd or live code, hjkl with count prefixes, ciw to change a word and :. to reindent turn a ten-minute edit into a one-minute one. Commands here are grouped by intent, moving, editing, searching and replacing, visual block and saving, rather than as an alphabetical list to memorise. A simple rule decides what to drill: whenever a task is "change this repeated text", reach for visual block or :%s, and confirm the hit count with a plain / search before committing. The closing group handles the two moments every newcomer hits, quitting without saving and writing a system file without sudo. Registers and macros handle the repetitive cases, and :copen turns the whole buffer of search results into something you can edit at a glance.
Motion 5
gg / G0 / $w / b:42Ctrl-f / Ctrl-bEdit 6
i / ao / Odd / yyp / Pu / Ctrl-rx / r<char>Search 4
/word:%s/old/new/g:%s/old/new/gc:nohVisual Mode & Save/Exit 6
v / VCtrl-v:w:wq or ZZ:q!:w !sudo tee %Command Examples
Save without quitting — the most common fix
:w编辑到一半需要存盘时用 :w,不必先 :q。配 :q! 可以丢弃改动强制退出,:wq 则是存盘并退出。
Output
"notes.md" 42L, 1834C written
Global replace with confirmation
:%s/old/new/gc
// c = confirm% 表示全文,g 表示行内全部,c 表示每次确认。批量改动时用 c 比直接 g 安全,误改能当场撤销。
Output
replace with new/new (y/n/a/q)?
Delete a whole line without entering insert mode
dd
3dd
yy
pdd 删当前行,3dd 删三行;yy 复制当前行,p 粘贴。Vim 的命令模式让你不用移动光标也能操作整行。
Search within the file and jump to matches
/pattern
n
:nohlsearch/ 向下搜、? 向上搜,n 跳下一个。:nohlsearch 清除高亮——搜索后满屏黄色会看不清代码。
Common Pitfalls
- In normal mode i, a, o, p and d are all commands and change the file immediately; press Esc whenever you are unsure.
- :q! discards all unsaved changes irrecoverably — know the difference between it and :wq.
- Typing :q while still in insert mode does nothing; press Esc first to return to normal mode.
- Most Vim commands are undoable with u, but unsaved changes are gone once you exit — use :w during long sessions.
- An IME can swallow keystrokes in Vim; switch to English input when editing files containing Chinese.
Tips
- Stuck in Vim? Press Esc to return to Normal mode, then type :q! to force quit without saving.
- Forgot sudo when editing a system file? Use :w !sudo tee % to save with elevated privileges without reopening.
- Before a large substitution, use /word to confirm matches, then :%s — add the c flag to confirm each replacement.
FAQ
How do I quickly switch between normal, insert, and command modes in Vim?
Vim opens in normal mode; press i/a/o to enter insert mode and type, Esc returns to normal mode for motion and editing, and typing a colon switches to command-line mode while / enters search. Just remember that normal mode is for operations and insert mode is for typing.
What should I do if I opened a file without sudo and cannot save my changes in Vim?
There is no need to quit and reopen. Type :w !sudo tee % to write the current file (the % stands for the current filename) with root privileges. If the buffer is read-only, reload it first with :e %. The cleanest fix is to open it with sudo vim from the start.
How do I replace all occurrences of foo with bar across the whole file in Vim?
Use :%s/foo/bar/g to replace globally; ^ and $ anchor the start and end of a line, and dropping the trailing g replaces only the first match per line. To confirm each substitution individually use :%s/foo/bar/gc and answer with y or n.
How do I undo an edit, and redo after undoing too far in Vim?
In normal mode press u to undo the last change and Ctrl+r to redo it. Vim keeps a multi-level undo history so you can press u repeatedly to step back. To discard all changes use :q! to force-quit without saving.
How do I insert or delete content at the same column across several lines in Vim?
In normal mode press Ctrl+v to enter block-visual mode, move vertically to select the same column on several lines, press I to start typing at the top, then press Esc so Vim replicates the text down the column; press d or x to delete the selected vertical range instead.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us