Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Almost by mistake, I figured out you could do something like:

$ cp foo.data bar1.data
$ ^bar1^bar2

And that runs the same command with bar2.data instead of bar1.data. Now, how about if I have multiple occurrences of the target word? For example:

$ cp foo.data bar.data
$ ^data^index

It only replaces the first data extension. How do I get it to replace both?

share|improve this question
up vote 20 down vote accepted

I think ^data^index is equivalent to !!:s/data/index, so it will only substitute the first word. If you want the whole line substituted, I think you'll have to use !!:gs/data/index/

share|improve this answer
    
This replacement (:gs/data/index) is a bash feature or is it implicitly calling another program? – JohnTortugo May 18 '15 at 0:59

You can do it by adding ^:& to the end.
^:& will replace two occurrences
^:g& will replace all

$ cp foo.data bar.data
$ ^data^index^:&
$ cp foo.index bar.index
$ cp foo.data bar.data joe.data doe.data
$ ^data^index^:g&
$ cp foo.index bar.index joe.index doe.index

sidenote: in the book 'command line kungfu'
it says that ^:& will replace all

share|improve this answer
1  
Actually, ^:& will do the substitution twice.  To do all occurrences, use ^:g& (g stands for "global"). – G-Man Jul 14 '15 at 20:07
    
Tnx ! I never tested it for more than two occurrences. Read the command in a book. – Note89 Jul 16 '15 at 6:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .