avp::ptr weblog

Everything that is in our mind.

Archive for April, 2012

Search and replace across multiple directories

without comments

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:

  1. Open vim while loading all relevant files recursively as a argument list the editor. vim **/*.txt
  2. Start recording a macro in register “a”. Type qa
  3. Enter the substitution command to search and replace a specific character pattern :%s/re\-use/reuse/ge
  4. Change to the next file in the argument list. Type :wnext
  5. Stop recording the macro in register “a”. Press q
  6. 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.

Written by tobi

April 7th, 2012 at 5:00 pm