Bash – Recover last argument of the last command in bash’ vi mode

bashinputrc

When using bash's vi mode (set -o vi), is it possible to recover the last argument of the last executed command? This is done in emacs mode with ESC+., and I would like to do it in vi mode as well.

I know that bash provides !$ and $_, but they are not expanded and I find quite dangerous to use them directly.

I've tried (with no success) some solutions I found on Stack Overflow about editing the .inputrc and adding:

set editing-mode vi
set keymap vi-insert
"\e.": yank-last-arg
"\e_": yank-last-arg

I'm switching to vi mode in bash but I'm quite used to ESC+. and it would be nice to be able to use it, or to find a quick & easy replacement.

EDIT: This question has been marked as a duplicate of a similar one that asks about how to recover last argument with Alt+S. I was asking specifically about ESC+. (it's the shortcut I'm used to and it is not covered by the other answer).

EDIT: To complement @chaos' solution: the following binding makes ESC+. (well, really '.') paste the last argument, but you lose Vi's dot (.) functionality:

bind -m vi-command ".":insert-last-argument

Best Answer

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+..

Related Question