Lesson 6 of 9 · 24m
Substitution, :global, and Controlled Repetition
Search-and-replace as a precision tool: regex modes, confirmation, capture groups, global commands, and when dot is the better automation.
Vim's substitution command is not just find-and-replace. It is a batch-editing language that can operate on ranges, reuse capture groups, and combine with :global to transform exactly the lines that matter. It is also very good at wrecking a file when you skip confirmation.
Start with the honest form
:%s/old/new/gc% means the whole buffer, g replaces every match on each line, and c asks before each replacement.
Use c until the regex has earned your trust. The time cost is seconds; the cost of a wrong capture group is an archaeology session through git diff.
Vim regex is not PCRE
Default Vim regex is “magic,” which means punctuation has mixed behavior. \v enables very magic mode and makes the syntax much closer to what programmers expect:
:%s/\v(\w+)\.save\(/save(\1, /gcIn very magic mode, parentheses group without backslashes and \1 reuses the first capture. Without \v, groups need escaped parentheses: \(...\). If you write regex in Vim all day, standardize your mental model around \v and say so in your notes.
Word boundaries matter too: :%s/\v<foo>/bar/gc uses \v< and \v> so replacing port does not rewrite transport or export_port.
The unsung precision tools: \zs and \ze
:%s/\vkey\s*=\s*\zs[^,]+/redacted/g\zs means “the actual replacement starts here.” Everything before it is context that must match but is not replaced. \ze ends the replaced region. These let you write replacements around stable surrounding text without rebuilding all of it in capture groups.
:global selects lines; :normal edits them
:g/^console\.log/d
:v/^\s*$/d
:g/TODO/normal! A — triage before release:g/pattern/command runs command on matching lines. :v inverts the match. The powerful form is :g/pattern/normal! ..., which executes normal-mode keystrokes on each matching line. This replaces thousands of fragile macros in structured files.
For example, append a semicolon to every line that starts an object property but lacks one:
:%s/\v^(\s+\w+\s*=\s*[^;]+)$/\1;/eThe e flag suppresses “pattern not found” errors in scripts and macros.
Dot is still the fastest automation
For irregular transformations, do one careful edit and press . at the next site. This workflow pairs naturally with search: perform ci"new value<Esc>, then / to the next candidate and press . only when it is appropriate. Automation with a human decision point beats blind batch replacement in code review-heavy work.
Field notes
&repeats the previous substitution on the current line;:%&repeats it across the file. Useful, but check flags first.:swithoutgchanges only the first match per line. People regularly think a replacement failed when the second match survived.- Use
\U,\L,\u, and\lin replacements for case transforms:\u\1uppercases the first character of group one. - Undo after a large substitution is granular enough to save you, but only if you notice immediately. Confirm first; trust later.
Was this lesson helpful?