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
<<using folders with latex |
Errorformat for java/ant/junit/cygwin/bash >>
Additional Notes
|