Vim logo vim online Vim Book Ad

basic Tip #373: Run find/replace/search on multiple files and subdirectories

 tip karma   Rating 5/11, Viewed by 681 

created:   November 22, 2002 10:02      complexity:   basic
author:   David Fishburn      as of Vim:   6.0


I wanted to recursively edit all html files in a folder/subfolders and run a search and replace command (substitute) in each one, then save the files when finished.

Vim 6.1.255

This is what my batch (WinXP) file does:
Note, since this is running in a windows batch/command file, I had to
escape the % sign.

First start a new instance of gvim, so it doesn't use one that I am already using.  
Give it a specific name so the commands are contained to it.
start gvim --servername BEAD

Using the FOR statement recursively edit all htm files and send them to the gvim session I just started:
FOR /R %%i IN (*.htm) DO gvim --servername BEAD --remote-silent "%%i"

Now, send the bufo command to that server that will run a substitute command.  
Note, I had to double up the % signs since I am in a batch/cmd file.
I also used the ge options so that no error was reported if the search string was not found.
gvim --servername BEAD --remote-send "<ESC>:bufdo %%s/Tweety Bird/Road
Runner/ge<CR>"

Now save all files and exit
gvim --servername BEAD --remote-send "<ESC>:xall<CR>"


Complete batch/cmd file (minus the ***'s)
***********************
start gvim --servername BEAD
FOR /R %%i IN (*.htm) DO gvim --servername BEAD --remote-silent "%%i"
gvim --servername BEAD --remote-send "<ESC>:bufdo %%s/Tweety Bird/Road
Runner/ge<CR>"
gvim --servername BEAD --remote-send "<ESC>:bufdo
%%s/\(^File:.*JPG\).*/\1/ge<CR>"
rem Write all files and exit
gvim --servername BEAD --remote-send "<ESC>:xall<CR>"
***********************

 rate this tip  Life Changing Helpful Unfulfilling 

<<Remove unwanted empty or blank lines for english and chinese | VIM's Filtering Commands Summary >>

Additional Notes

Anonymous, November 25, 2002 3:48
See :args and :argdo
Anonymous2, December 3, 2002 12:55
How to use :args to search recursive in subdirectories?
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo