How do I run a command on every file in the quickfix list?
Answer
:cdo s/old/new/g | update
Explanation
The :cdo command executes a given command on every entry in the quickfix list. Combined with :update (which saves only if the file changed), this creates a powerful multi-file batch editing workflow.
How it works
- Populate the quickfix list (via
:vimgrep,:grep, or a plugin) - Run
:cdowith your command - Each quickfix entry is visited and the command is executed
Example: Multi-file search and replace
" Step 1: Find all occurrences
:vimgrep /oldFunction/g **/*.js
" Step 2: Replace in every match and save
:cdo s/oldFunction/newFunction/g | update
cdo vs cfdo
| Command | Scope |
|---|---|
:cdo |
Runs once per entry (line) in quickfix |
:cfdo |
Runs once per file in quickfix |
" cfdo is more efficient when every file needs the same change
:cfdo %s/old/new/g | update
" cdo is better when you need per-match precision
:cdo s/old/new/ | update
Tips
- Always append
| updateto save each file after modification - Use
:vimgrepor an external grep to populate the quickfix list first :ldoand:lfdoare the location-list equivalents- This workflow replaces the need for
sed -iacross multiple files - Add
eflag to suppress errors on files without matches:s/old/new/ge