Bash – How to enable yank-nth-arg using vi mode on Bash

bashkeyboard shortcutsvi-mode

Hey guys I'm trying to figure out how can I take my nth arg, from my last command, without using the !:nth. In a normal bash (emacs mode) I can do that using the follow shortcuts:

<ESC>nth_arg <ESC><c-y>

how can I do the same using the bash vi mode (bash -o vi) ?

my relevant .bashrc lines

set -o vi
#BASH yank-nth-arg: <esc>narg <c-a>y
#BASH yank-last-arg: <a-.>
bind -m vi-insert '"\e.": yank-last-arg'

my current binds with yank in it:

$ bind -lp | grep 'yank'
vi-yank-arg
vi-yank-to
yank
yank-last-arg
yank-nth-arg
yank-pop
# vi-yank-arg (not bound)
# vi-yank-to (not bound)
"\C-y": yank
"\e.": yank-last-arg
# yank-nth-arg (not bound)
# yank-pop (not bound)

currently when I try to do the:

<ESC>1 <ESC><c-y> or <ESC>1 <ALT-c-y>

I get a space/tab, or nothing happens after the (arg: 1) prompt disappears.

BR

Best Answer

You can also use below bind key:

bind -m vi-insert '".": yank-last-arg'

or:

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

To get the nth arguments:

bind -m vi-command '"\e-": yank-nth-arg'

Now you can use <ALT>n <ALT>- to get nth argument from previous command.

Related Question