How do I run an interactive replacement across quickfix entries and only save changed files?
Answer
:cdo keeppatterns s/\<foo\>/bar/gec | update
Explanation
When quickfix already contains precise targets, :cdo gives you a safer multi-file replace loop than broad project substitutions. This variant adds confirmation and conditional writes, so you can review each hit and persist only files that actually changed. It is ideal for high-risk renames where false positives are expensive.
How it works
:cdoexecutes a command for each quickfix entrykeeppatternsavoids overwriting your last search pattern registers/\<foo\>/bar/gecruns substitution on the current line\<and\>enforce whole-word matchinggreplaces all matches on that lineesuppresses pattern-not-found errorscasks for confirmation at each match| updatewrites only if the current buffer was modified
Example
Start from a curated quickfix list:
:vimgrep /\<foo\>/gj src/**/*
:copen
:cdo keeppatterns s/\<foo\>/bar/gec | update
Vim steps through each quickfix location, prompts for each replacement, and only saves touched files.
Tips
- Use
:cfdoinstead of:cdowhen you want one execution per file, not per match - Drop
cafter a dry run when you are confident - Combine with
:colderand:cnewerif you maintain multiple quickfix snapshots