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

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

  1. Populate the quickfix list (via :vimgrep, :grep, or a plugin)
  2. Run :cdo with your command
  3. 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 | update to save each file after modification
  • Use :vimgrep or an external grep to populate the quickfix list first
  • :ldo and :lfdo are the location-list equivalents
  • This workflow replaces the need for sed -i across multiple files
  • Add e flag to suppress errors on files without matches: s/old/new/ge

Next

How do I always access my last yanked text regardless of deletes?