How do I run a substitution over every quickfix hit and save only changed files?
:cdo s/foo/bar/ge | update
When quickfix already contains exactly the lines you want to touch, :cdo is the safest way to batch-edit with tight scope.
category:
command-line
tags:
#command-line
#quickfix
#substitution
#refactoring
How do I count substitution matches before changing anything?
:%s/pattern/replacement/gn
When you are about to run a broad substitution, it is often safer to measure impact first.
category:
command-line
tags:
#command-line
#ex-commands
#substitution
#search
How do I title-case every word in a buffer using one Vim substitution?
:%s/\v<(\w)(\w*)>/\u\1\L\2/g
When you need to normalize casing across headings, labels, or generated docs, editing words one by one is tedious.
category:
editing
tags:
#editing
#substitution
#regex
#formatting
How do I zero-pad every number in a buffer with one substitution?
:%s/\v(\d+)/\=printf('%04d', submatch(1))/g
When you need stable-width numeric fields, manual edits are slow and error-prone.
category:
command-line
tags:
#command-line
#substitution
#regex
#refactoring
How do I remove trailing spaces only within the currently selected visual block?
Sometimes you need to clean alignment artifacts in a rectangular region without touching the rest of each line.
category:
visual-mode
tags:
#visual-mode
#substitution
#regex
#formatting
#editing
How do I swap the first two whitespace-separated columns on every line?
:%s/\v(\S+)\s+(\S+)/\2 \1/<CR>
When you need to flip two fields across a whole file, a single substitution is faster and safer than recording a macro.
category:
editing
tags:
#editing
#substitution
#regex
#ex-commands
#formatting
How do I run a full-buffer substitution without disturbing marks, jumplist, or search history?
:lockmarks keepjumps keeppatterns %s/foo/bar/ge
Large substitutions are efficient, but they often leave side effects: your last search changes, your jumplist gets noisy, and marks can shift in ways that break
category:
command-line
tags:
#command-line
#editing
#substitution
#navigation
How do I zero-pad every number in a line using a Vim substitute expression?
:s/\v\d+/\=printf('%04d', submatch(0))/g
Substitution expressions let Vim compute each replacement dynamically, which is ideal when plain capture groups are too limited.
category:
editing
tags:
#editing
#substitution
#regex
#vimscript
How do I uppercase only the characters inside a visual selection with :substitute?
When you need to transform text in-place without touching surrounding content, \%V is one of Vim's most precise tools.
category:
visual-mode
tags:
#visual-mode
#substitution
#search
#editing
How do I strip trailing whitespace without clobbering my last search pattern?
:keeppatterns %s/\s\+$//e
Bulk cleanup commands often damage your navigation flow by overwriting the last search pattern (@/).
category:
command-line
tags:
#command-line
#search
#substitution
#whitespace
How do I use an expression in a substitution to perform arithmetic on matched numbers?
:%s/\(\d\+\)/\=submatch(1)+1/g
Vim's substitute command supports using a VimScript expression as the replacement by prefixing it with \=.
category:
search
tags:
#search
#substitution
#expressions
#editing
How do I reuse the replacement text from my last substitution in a new one without retyping it?
In a Vim substitution, using ~ as the replacement string repeats the replacement text from the most recent :s command.
category:
search
tags:
#search
#substitution
#ex-commands
#editing
How do I zero-pad or reformat numbers during a substitution using printf in Vim?
:%s/\d\+/\=printf('%03d', submatch(0))/g
Combining Vim's \= expression substitution with the printf() function lets you apply arbitrary formatting to matched text.
category:
search
tags:
#search
#substitution
#ex-commands
#editing
How do I replace only the current match and then stop when doing an interactive substitution in Vim?
l (in :%s///gc confirm prompt)
When running an interactive substitution with the c flag (e.
category:
editing
tags:
#search
#editing
#substitution
#ex-commands
#normal-mode
How do I convert text to title case in Vim using a substitution command?
Vim's substitution engine supports case-modifier sequences in the replacement string, making it possible to convert text to title case in a single command.
category:
search
tags:
#search
#substitution
#regex
#editing
#case
#formatting
How do I restrict a substitution to the range between two named marks?
Named marks can serve as range endpoints for any Ex command, including :substitute.
category:
search
tags:
#search
#marks
#substitution
#ex-commands
#editing
How do I match text only when it is preceded by a specific pattern in Vim regex?
:%s/\(prefix\)\@<=target/replacement/g
Vim's \@<= is a zero-width look-behind assertion.
category:
search
tags:
#search
#regex
#substitution
#advanced
How do I use \zs and \ze to define the exact boundaries of a regex match in Vim?
Vim's \zs ("match start") and \ze ("match end") atoms let you narrow the actual match region within a broader pattern context.
category:
search
tags:
#search
#regex
#substitution
#patterns
#advanced
What is the difference between \n and \r in Vim search patterns versus substitution replacements?
\n in search, \r in replacement
Vim uses \n and \r differently depending on whether they appear in a search pattern or a replacement string, and mixing them up is a common source of confusion.
category:
search
tags:
#search
#substitution
#newline
#patterns
#editing
How do I match a pattern only when preceded or followed by another pattern using zero-width assertions?
Vim's regex engine supports zero-width lookahead and lookbehind assertions — \@= and \@<= — which let you match text based on surrounding context without in
category:
search
tags:
#search
#regex
#advanced
#patterns
#substitution