I would like to improve my workflow a bit with Bash, and I realize I often want to execute the same command to a different executable.
Some examples:
Git (where I would like to quickly change the second word):
git diff foo/bar.c
git checkout foo/bar.c
Cat/rm (where only the first word has to be changed):
cat foo/is/a/very/long/path/to/bar.c
rm foo/is/a/very/long/path/to/bar.c
I know I can hit Ctrl+a then Del to remove the first word, but I am wondering if there is a quicker way to do it.
Best Answer
!$
expands to the last word of your previous command.So you could do:
or:
Your examples happened to only repeat the last word, so
!$
worked fine. If you actually had a lot of arguments that you wanted to repeat, and you just wanted to change the first word, you could use!*
, which expands to all words of the previous command except the zeroth.See the "HISTORY EXPANSION" section of the
bash
man page. There's a lot of flexibility there.