Vim Cheatsheet - Editor Command Reference
On a headless server, Vim is the tool you cannot avoid for editing config files. These operations cover precise cursor motion, batch replace, block visual editing, and saving with sudo when you forgot to elevate. Spend ten minutes making motion and search/replace muscle memory, then editing nginx or sshd configs gets naturally faster.
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 %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