Cheatsheet Pages / Vim Cheatsheet
How to find and replace across a file or a line range in Vim — :%s and :12,20s
A rename that touches the whole file — or just lines 12 to 20 — in a single command.
Almost every entry was run headless in Vim 9.1 and the resulting file checked; the few entries a tiny build cannot run (system clipboard, persistent undo, the sudo save) were verified against Vim's own documentation instead.
:%s/foo/bar/g " every line in the file: replace all foo with bar
:12,20s/foo/bar/g " only lines 12 to 20
Good to know
⚠️ :%s reaches the whole file — one wrong pattern rewrites hundreds of lines before you notice. When in doubt, add the c flag (:%s/foo/bar/gc) and confirm match by match; u undoes the whole substitution if it went wrong.
