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
- Populate the quickfix list — e.g.,
:vimgrep /pattern/ **/*.goor by running a build - Run
:cdo {cmd}— Vim jumps to each entry and executes{cmd}there - Add
| updateto save each file after the command runs
The distinction between :cdo and :cfdo:
:cdo s/old/new/— runss/old/new/at each line in the quickfix list (fine-grained):cfdo %s/old/new/g— runs%s/old/new/gonce 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 @qto replay a macro at each quickfix location - Combine with
:copento review entries before running:cdo :ldois the location-list equivalent (operates on:lgrep/:lvimgrepresults)- After a substitution, run
:cfdo updateif you want to ensure all modified files are saved even if:cdoalready did it