vimtricks.wiki Concise Vim tricks, one at a time.

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

  • :cdo executes a command for each quickfix entry
  • keeppatterns avoids overwriting your last search pattern register
  • s/\<foo\>/bar/gec runs substitution on the current line
  • \< and \> enforce whole-word matching
  • g replaces all matches on that line
  • e suppresses pattern-not-found errors
  • c asks for confirmation at each match
  • | update writes 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 :cfdo instead of :cdo when you want one execution per file, not per match
  • Drop c after a dry run when you are confident
  • Combine with :colder and :cnewer if you maintain multiple quickfix snapshots

Next

How do I launch GDB inside Vim using the built-in termdebug plugin without preloading it?