With bash you can do nearly what you're asking for like this:
echo !echo:$
So when you do
echo This is fun
ls
echo !echo:$
the last line outputs fun
. *
instead of $
produces all the arguments to the matching command; so
echo This is fun
ls
echo !echo:*
outputs This is fun
again; but you might as well just do
!echo
in this case.
This isn't limited to repeating a command's arguments with the same command:
printf !echo:*
See the bash reference manual for details. You could even combine this with the histverify
shell option to give you a chance to check the command before it's executed, which gets you close to keyboard-interactive history-based completion.
History expansions can be used anywhere; so for example
lastechoargs="!echo:*"
stored all the arguments to the last echo
command in the lastechoargs
variable.
This works for complete commands too; say for example you've worked out a complex git
command, and you want to save it in a file:
echo !git > mygitcommand
Or you want to archive some directories, but you decide to delete a couple of files first:
ls dir1 dir2
rm dir2/somefile
tar cpzf archive.tar.gz !ls:*
For me it works when I add the following to my .inputrc
:
$if mode=vi
"\e.":yank-last-arg
$endif
Then, when changing it in bash on the fly, the .inputrc
must be read again:
set -o vi
bind -f .inputrc
Now, I can get the last argument with alt+..
Best Answer
Add this line after
set -o vi
:Then you can use Alt+. like in emacs-mode.
Or use history expansion, working in both: