sponsor Vim development Vim logo Vim Book Ad

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

 tip karma   Rating 37/30, Viewed by 8815 

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?
Anonymous, March 4, 2003 5:19
you can use something like this:
:args *.cpp */*.cpp */*/*.cpp

Not exactly *recursive*, but works well for reasonably flat directory hierarchies.
[email protected], June 18, 2003 10:26
Fantastic Tip.  Saved me a lot of time.
I had put the [ ! ] after the bufdo command so it would move on to the next buffer.

gvim --servername BEAD --remote-send "<ESC>:bufdo! %%s/Tweety Bird/Road Runner/ge<CR>"

Thanks.
mbvlist at fastmail dot fm, February 10, 2005 1:55
Using windows XP it caused some weird problems. I solved it by running gvim using start every time, like the first time. I also added some sleep statements, but i'm not shure if it's neccesary.
[email protected], June 26, 2005 1:04
This is the right Tip i am looking for .
But i was wondering it's behavior when i used it.
Sometimes it goes well and sometimes it does not.
it seems that  there should be some sleep/wait command between the
commands of Gvim.

e.g.
FOR /R %%i IN (*.aspx) DO start L:\vim\vim63\gvim --servername BEAD --remote-silent "%%i"
@rem maybe need some wait?
sleep -m 2000
start L:\vim\vim63\gvim --servername BEAD --remote-send "<ESC>:bufdo! %%s/cancel_button/button_cancel/ge<CR>"


But , i cannot find sleep or wait command under windows2000 srv.
who can help?

thanks.
Anonymous, November 9, 2005 11:11
if you have some hudreds of files, this is simple unusable.

you have to watch it showing "pattern not found" for dozens of files, wich will make it hang when it uses the entire screen until you press space.
Anonymous, November 22, 2005 5:25
In a *nix shel, as found on http://www.jesus.com.au/html/page/vim :

for i in `find . -name '*.txt'`; do vim -c "%s/nonsenes/nonsense/g | w | q" "$i"; done
(Obviously, do NOT execute that exact command, or anything like it, unless you know what you're doing.)
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.
    stats
Sponsored by Web Concept Group Inc. SourceForge.net Logo