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

How do I run a command on every individual quickfix entry in Vim?

Answer

:cdo s/old/new/ | update

Explanation

:cdo {cmd} executes a command at every entry in the quickfix list, visiting each location in turn. Unlike :cfdo (which operates once per file), :cdo lands the cursor on each exact match location — making it ideal when you want per-occurrence control rather than file-wide substitutions.

How it works

  1. Populate the quickfix list — e.g., :vimgrep /pattern/ **/*.go or by running a build
  2. Run :cdo {cmd} — Vim jumps to each entry and executes {cmd} there
  3. Add | update to save each file after the command runs

The distinction between :cdo and :cfdo:

  • :cdo s/old/new/ — runs s/old/new/ at each line in the quickfix list (fine-grained)
  • :cfdo %s/old/new/g — runs %s/old/new/g once per file (file-wide)

Example

Find all calls to a deprecated function across a project:

:vimgrep /oldFunc/ **/*.js
:cdo s/oldFunc/newFunc/ | update

This replaces oldFunc at each specific quickfix location, then saves the file.

Tips

  • Use :cdo norm @q to replay a macro at each quickfix location
  • Combine with :copen to review entries before running :cdo
  • :ldo is the location-list equivalent (operates on :lgrep / :lvimgrep results)
  • After a substitution, run :cfdo update if you want to ensure all modified files are saved even if :cdo already did it

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?