sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #324: Search and replace in files named NAME

 tip karma   Rating 30/12, Viewed by 2900 

created:   September 7, 2002 14:34      complexity:   intermediate
author:   Luis Mondesi      as of Vim:   5.7

I'm not sure if there is a simple way to do this from within Vim, but, I wrote this simple script that does it. It basically searches for files named NAMED (whatever name pass) for a given string and replaces that with a given string:
find_replace.sh NAMED "string_to_find" "string_to_replace"

This is all done from the command line without opening Vim.

Of course one could do things like:
                    :let n = 1
                    :while n <= argc()      " loop over all files in arglist
                    :  exe "argument " . n
                    :  " start at the last char in the file and wrap for the
                    :  " first search to find match at start of file
                    :  normal G$
                    :  let flags = "w"
                    :  while search("foo", flags) > 0
                    :    s/foo/bar/g
                    :    let flags = "W"
                    :  endwhile
                    :  update               " write the file if modified
                    :  let n = n + 1
                    :endwhile

As suggested in the Vim help files :-) but, I wanted to go and find only these files... here is the script:
      1 #!/bin/sh
      2 # Luis Mondesi < [email protected] >
      3 # DESCRIPTION:
      4 #   it uses vim to replace a given string for
      5 #   another in a  number of files
      6 #
      7 # usage:
      8 #   find_replace.sh file "string" "replace"
      9 #
     10 if [ $1 -a $2 -a $3 ]; then
     11     for i in `find . -name "$1" -type f | xargs grep -l $2`; do
     12         # how do search and replace
     13         # the screen might flicker... vim opening and closing...
     14         vim -c ":%s/$2/$3/g" -c ":wq" $i
     15     done
     16     exit 0
     17 fi
     18 # I should never reach here
     19 echo -e "USAGE: find_replace.sh file 'string' 'replace' \n\n"
     20 exit 1

 rate this tip  Life Changing Helpful Unfulfilling 

<<using folders with latex | Errorformat for java/ant/junit/cygwin/bash >>

Additional Notes

[email protected], September 8, 2002 8:19
If you have a shell script environment available to you, you must also have sed.  Why not just pipe the output of the find to xargs sed -e "%s/<from>/<to>/g"?

(Nothing against Vim; just trying to use the right tool for the job.)
Anonymous, September 15, 2002 11:48
Not an option on a default install of Windows ;o)
Anonymous, October 11, 2002 7:40
What would make this feature more interesting is to be able to run from inside VIM, ie I'm editing several files from a tree inside VIM, I want the text replaced both inside the file I'm currently editing (and the VIM buffers updated) and all the others from that tree.
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.
Sponsored by Web Concept Group Inc. SourceForge Logo