Vim Recall繁體中文

Edit only quickfix entry lines

:cdo s/old/new/ge | update

Example

Normal mode. Checked quickfix list: line 1 of a.txt and b.txt. Both files have old on lines 1 and 2. Both will be saved.

Before
Quickfix entries: a.txt line 1, b.txt line 1 Each file: line 1 = old; line 2 = old

  1. :cdo s/old/new/ge | update<CR> Replace old on each listed line and save; line 2 stays old

After
a.txt: line 1 = new; line 2 = old b.txt: line 1 = new; line 2 = old Changed files: saved

Details & Vim help

Run a line substitution at every valid quickfix entry.

Limit a bulk edit to the lines found by a search.

Multiple entries can target the same line and run the command repeatedly. update writes all pending changes in each visited buffer. Avoid commands whose repeated application would compound the edit.

:cdo visits each entry. With no % range, this substitution affects the entry’s line.

Try it in Vim

  1. For this example, put old on lines 1 and 2 of a.txt and b.txt. Run :vimgrep /\%^old/j a.txt b.txt to list only line 1 of each file. Open the list with :copen.
  2. Use :cdo s/old/new/ge | update to edit each entry’s current line and save changes.
  3. Review the resulting diff.