Vim Recall繁體中文

Apply one edit per quickfix file

:cfdo %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 files: a.txt, b.txt Each file: line 1 = old; line 2 = old Only line 1 is listed as a quickfix entry.

  1. :cfdo %s/old/new/ge | update<CR> Replace old on both lines in both files; save changes

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

Details & Vim help

Run a buffer-wide edit once for each file in the current quickfix list.

Use search results as the file set for a coordinated replacement.

%s affects entire listed files, beyond listed matches. update writes each changed buffer. Commit or back up first; other unsaved changes in those buffers will also be written. Use :cdo for entry-by-entry work.

:cfdo visits each listed file. %s replaces matches throughout that whole file, including unlisted lines.

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. Check the files and replacement on a small sample.
  3. Run :cfdo %s/old/new/ge | update when you intend to save each changed file.