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

How do I run a command on every line in the quickfix list at once?

Answer

:cdo {cmd}

Explanation

:cdo executes an Ex command on every entry in the quickfix list in sequence, visiting each match in turn. Combined with :vimgrep or :grep, it enables precise, line-targeted transformations across an entire project — without touching lines that were not matched.

How it works

  • :cdo — iterate over each quickfix entry (one per matched line) and run {cmd} on it
  • This is distinct from :cfdo, which iterates per file in the quickfix list. Use :cdo when you want line-level control; use :cfdo when you want to run a whole-file %s/// on each affected file.

Typical workflow:

:vimgrep /old_api/gj **/*.py
:cdo s/old_api/new_api/g | update
  1. :vimgrep populates the quickfix list with every match
  2. :cdo s/old_api/new_api/g performs the substitution on each matched line
  3. | update saves files that were modified

Example

Find all TODO comments and append a date:

:vimgrep /TODO/gj **/*.md
:cdo s/TODO/TODO (2026-01-01)/g | update

Only lines containing TODO are modified — untouched lines in those files stay exactly as they are.

Tips

  • Add the e flag to :substitute to suppress "pattern not found" errors on entries where the pattern doesn't match: :cdo s/old/new/ge | update
  • Use :ldo to do the same with the location list instead of the quickfix list
  • Open the quickfix window with :copen first to review matches before running :cdo
  • Combine with :cfdo update afterward if you prefer to batch the saves: :cdo s/old/new/g then :cfdo update

Next

How do I rename a variable across all its case variants (camelCase, snake_case, SCREAMING_CASE) in one command?