I\'m looking for the best way to do search-and-replace (with confirmation) across all project files in Vim. By \"project files\" I mean files in the current directory, some
maybe do this:
:noautocmd vim /Search/ **/*
:set hidden
:cfirst
qa
:%s//Replace/gce
:cnf
q
1000@a
:wa
Explanation:
:noautocmd vim /Search/ **/* ⇒ lookup (vim is an abbreviation for vimgrep) pattern in all files in all subdirectories of the cwd without triggering autocmds (:noautocmd), for speed's sake.:set hidden ⇒ allow having modified buffers not displayed in a window (could be in your vimrc):cfirst ⇒ jump to first search resultqa ⇒ start recording a macro into register a:%s//Replace/gce ⇒ replace all occurrences of the last search pattern (still /Search/ at that time) with Replace:
g flag)c flag)e flag):cnf ⇒ jump to next file in the list created by the vim commandq ⇒ stop recording macro1000@a ⇒ play macro stored in register a 1000 times:wa ⇒ save all modified buffers* EDIT * Vim 8 way:
Starting with Vim 8 there is a better way to do it, as :cfdo iterates on all files in the quickfix list:
:noautocmd vim /Search/ **/*
:set hidden
:cfdo %s//Replace/gce
:wa