In Bash, there are some handy operators to repeat parts of the last command:
!^
expands to the first argument of the previous command, e.g.,$ echo one "two three" one two three $ echo !^ echo one one
!$
expands to the last argument of the previous command, e.g.,$ echo one "two three" one two three $ echo !$ echo "two three" two three
!*
expands to all arguments of the previous command, e.g.,$ echo one "two three" one two three $ echo !* echo one "two three" one two three
(As far as I understand, these are syntactic sugar for !!:^
, !!:$
and !!:*
respectively, where !!
is an event designator that expands to the previous command, and ^
, $
and *
are word designators, see the Bash Reference Manual or man bash
.)
These are often quite handy. But it gets even cooler with keyboard shortcuts:
When you hit Alt+. or Alt+_, the last argument of the previous command is inserted in the current command, similarly as if you had written
!$
at that point.It is also possible to hit Alt+Ctrl+y to insert the first argument of the previous command, as if you had written
!^
at that point.
(See the GNU Readline Library or info readline
.)
I tend to prefer the keyboard shortcuts over Bash's history operators, because I can see what I'm inserting before I actually execute the command. However, there does not seem to be a shortcut that enables me to insert all the words of the previous command, i.e., one that does !*
's job. At least I could not find it.
Is there such a shortcut? If not, is it possible to configure the readline library to add one, and how?
zsh
, if you typeecho !*
and then press TAB, you have the desired effect. In general, TAB while in readline will expand all the expandable. Very handy; probably bash will have some configuration to the same effect? @Seth, I think this is the readline into bash, not the terminal emulator - not sure though.man bash
(somewhere at the line 3030)\e.
and\e_
are mapped to readline functionyank-last-arg
and\e\C-y
is mapped toyank-nth-arg
. Sadly there seems to be no (single) command that adds multiple previous arguments at once.