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.

SysOps·34 commands·Last updated 2026-07-21

Motion 5

gg / G
Jump to top / bottom of file
0 / $
Jump to start / end of line
w / b
Forward / backward one word
:42
Jump to line 42
Ctrl-f / Ctrl-b
Page down / page up

Edit 6

i / a
Enter insert mode before / after cursor
o / O
New line below / above and enter insert mode
dd / yy
Delete / yank (copy) current line
p / P
Paste after / before cursor
u / Ctrl-r
Undo / redo
x / r<char>
Delete character / replace single character

Visual Mode & Save/Exit 6

v / V
Character-wise / line-wise visual selection
Ctrl-v
Block visual mode for column editing
:w
Save
:wq or ZZ
Save and quit
:q!
Force quit without saving
:w !sudo tee %
Force save when forgot sudo

Multiple Files & Windows 6

:e file
Open or switch to another file
:ls or :buffers
List every open buffer
:bn / :bp
Jump to the next / previous buffer
:sp file / :vsp file
Split horizontally / vertically
Ctrl-w w / Ctrl-w hjkl
Move between split windows
:tabnew file / gt
New tab / next tab

Macros & Registers 7

qq
Record a macro into register q, press q again to stop
@q
Play back the macro stored in register q
@@
Repeat the last executed macro
10@q
Play the macro ten times in a row
"ayy
Yank the current line into register a
"+p
Paste from the system clipboard (needs +clipboard)
:reg
Show the contents of all registers

Command Examples

Save without quitting — the most common fix

:w

Use :w to flush the buffer without leaving the file, so there is no reason to quit first. Pair it with :q! to discard changes and force out, or :wq to do both in one step.

Output

"notes.md" 42L, 1834C written

Global replace with confirmation

:%s/old/new/gc // c = confirm

% means the whole file, g means every match on a line, c asks before each replacement. On a bulk edit the c variant is far safer than a bare g because a mistaken replacement is caught, and undone, on the spot.

Output

replace with new/new (y/n/a/q)?

Delete a whole line without entering insert mode

dd 3dd yy p

dd removes the current line and 3dd removes three; yy copies the current line and p pastes it. Operator-pending commands let you work on whole lines without ever placing the cursor at one end of them.

Search within the file and jump to matches

/pattern n :nohlsearch

/ searches forward and ? backward, with n stepping to the next hit. :nohlsearch clears the highlighting, which matters because a fully highlighted screen is unreadable while you work.

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