views:

195

answers:

3

To clarify, I am looking for a way to perform a global search and replace on the previous command used. ^word^replacement^ only seems to replace the first match.

A quick check through a BASH history cheat sheet doesn't reveal anything. Is there some set option that is eluding me?

Mainly curious...

Thanks

+1  A: 

A nasty way to get around this could be something like this:

Want to echo BAABAA rather than BLABLA by swapping L's for A's

$ echo "BLABLA"   
BLABLA
$ `echo "!!" | sed 's/L/A/g'`
$(echo "echo "BLABLA" " | sed 's/L/A/g')
BAABAA
$

Unfortunately this technique doesn't seem to work in functions or aliases.

pisswillis
+2  A: 

Try this:

$ echo oneone
oneone
$ !!:gs/one/two/    # Repeats last command; substitutes 'one' --> 'two'.
twotwo
John Feminella
This does not work for me. I get 'twoone'
finnw
Are you sure you typed it right? It works fine for me. (I'm using GNU bash, version 3.2.48(1)-release.)
John Feminella
A: 

Blending my answer here with John Feminella's you can do this if you want an alias:

$alias dothis='`history -p "!?monkey?:gs/jpg/png/"`'
$ls *.jpg
monkey.jpg
$dothis
monkey.png

The !! only does the previous command, while !?string? matches the most recent command containing "string".

Dennis Williamson