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

How do I search and replace across multiple files using the quickfix list?

Answer

:cdo s/old/new/g

Explanation

The :cdo command executes a command on every entry in the quickfix list. Combined with :vimgrep, this enables multi-file search and replace.

How it works

  1. Populate the quickfix list: :vimgrep /old/ **/*.js
  2. Run substitute on each entry: :cdo s/old/new/g
  3. Save all changed files: :cfdo update

Example

:vimgrep /deprecated_func/ **/*.py
:cdo s/deprecated_func/new_func/g
:cfdo update

This replaces deprecated_func with new_func across all Python files.

Tips

  • :cfdo runs the command once per file (not per match)
  • :cdo runs the command once per quickfix entry
  • Use :cfdo update to save only modified files
  • :bufdo can also work for search/replace across all open buffers

Next

How do you yank a single word into a named register?