I've been a (blind) user of bash's "caret replacement" for ages, but have occasionally wondered how to replace all occurences of a word, rather than the first.
$ echo "foo bar baz foo" foo bar baz foo $ ^foo^quux echo "quux bar baz foo" quux bar baz foo
The answer is to use the more robust event designator syntax: !!:gs/search/replace/
$ echo "foo bar baz foo" foo bar baz foo $ !!:gs/foo/quux/ echo "quux bar baz quux" quux bar baz quux
While on the topic of esoteric bash commands, here's another good one. You may (?) know of !$ which references the last argument of the last command in history. That's just an alias for !!:$, which shows the more general form of the command. You can access any argument of any command in history.
$ echo "test" > /tmp/blah.txt $ cat !!:3 cat /tmp/blah.txt test
There's lots more. In particular, you can operate on any command in history, not just the last one (!!). See the bash manual link in the references below.
References
- History Interaction in the bash manual
- "Caret search and replace" question at StackOverflow
"set -o vi" is good enough for short commands, but !!:gs is great for long ones. Thanks Mark!
ReplyDelete