Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] search and replace multiple files?

On Tue, Aug 02, 2011 at 02:24:47PM +1000, Sonia Hamilton wrote:
> * Nick Andrew <nick@xxxxxxxxxxxxxxx> [2011-08-02 12:23:43 +1000]:
> > On Tue, Aug 02, 2011 at 10:29:44AM +1000, Sonia Hamilton wrote:
> > > For example, I want to replace "def fubar" with "def snafu" across 50
> > > files.
> > 
> > perl -p -i -e 's/def fubar/def snafu/g' *
> I said: "PS I know about sed, and how to edit multiple files in vim
> [1]." Supplying a perl recipe therefore isn't very useful. Perhaps I
> should've said "ditto for ruby, perl, python, ...".

I know you know about sed but I don't know you know how to edit multiple
files in-place in a single command in perl. sed is a pain to edit files
in-place as it has to write to a temp file and then typically rename that
temp file to replace the original file.

> > If you are not using SCM (or git in particular), then you should be.
> http://soniahamilton.wordpress.com/tag/git/

Nice site.

> Again, not the question I was asking...

I don't know how to get your editor to do it and "leave only changed files
open". I suppose it's possible using vim macros but it's not something I
have spent any time learning. I know you know how to edit multiple files
in vim but this is one of the more trivial features of the editor and I
don't know that you know anything about vim macros or have any desire to
learn what is probably a pretty complicated solution for a problem which
can be solved easily with two steps.

I would myself have done the refactoring exactly how I showed it, and
let git keep track of what files have changed.

If it's a problem with leaving the editor open with only changed files,
then I can suggest something like this:

  vim -o $(git status --porcelain | awk ' /^ M/ { print $2 } ')

PGP Key ID = 0x418487E7                      http://www.nick-andrew.net/
PGP Key fingerprint = B3ED 6894 8E49 1770 C24A  67E3 6266 6EB9 4184 87E7