Search and replace across multiple directories
Today, I tried to find a convenient way to unify the spelling of a word used in various text files spread across multiple directories. Of course vim has a solution to this. In my case all files are of type *.txt. The term I want to search for is “re-use” which should be replaced by “reuse“. Here is what I did:
- Open vim while loading all relevant files recursively as a argument list the editor.
vim **/*.txt
- Start recording a macro in register “a”. Type
qa
- Enter the substitution command to search and replace a specific character pattern
:%s/re\-use/reuse/ge
- Change to the next file in the argument list. Type
:wnext
- Stop recording the macro in register “a”. Press
q
- Playback the macro 999-times by entering.
999@a
The posts “Vim 101: Search and Replace on Multiple Files” and “Vim. How to replace a word (string) in several files.” guided me. Please have a look there for further information.