23

I have been convinced (over at Stack Overflow) to use my beloved bash in vi mode. So far I got used to it quite well and I like it.

However I really do miss one feature: In emacs-mode, you can enter the last parameter of the previous command by pressing "ESC ." (That is, press escape followed by the .)

Is there a default binding to insert the last parameter in vi-mode? I wasn't able to find one and I really miss this command...

6 Answers 6

10

There's no default. The binding for 'yank-last-arg' (as listed by 'bind -p') disappears when you switch to vi mode.

bind '"\e."':yank-last-arg

will give you that same binding back (or pick something else)

2
  • 2
    clone for zsh bindkey "\e." insert-last-word
    – jhvaras
    Commented Jul 23, 2015 at 12:53
  • I've had it for years on zsh and every time I'm forced to use bash (e.g. on a server at work) I keep forgetting I never figured out how to set it up on bash. No more! This in combination with history-search-backwards is invaluable for serious command line users. Commented Sep 12, 2017 at 21:11
13

There's (vi-yank-arg), by default mapped to "_". That should do what you want (in command mode).

2
  • 1
    +1. I am not sure why this answer was not accepted.
    – Sathyam
    Commented Mar 7, 2017 at 15:05
  • Just mind you need to be in normal mode, not in insert. Commented Apr 28, 2021 at 21:33
8

Not exactly the same, but in either mode you can type !$, and it will be replaced by the last word of the previous command. Find more such things in the manual.

1
  • Close enough to get an upvote but not exactly the same... I miss the possibility to edit the line before executing it. But thanks alot anyway!
    – Mo.
    Commented Aug 5, 2009 at 14:17
5

The ~/.inputrc file can configure the key-map (You can generate it by manual if it cannot be found). Add the following command into the ~/.inputrc:

"\e.": yank-last-arg
"\e_": yank-last-arg

My ~/.inputrc is:

set completion-ignore-case on
set show-all-if-ambiguous on
set show-all-if-unmodified on

set editing-mode vi
set keymap vi-insert

"\C-p": previous-history
"\C-n": next-history
"\C-a.":beginning-of-line
"\C-e.":end-of-line

"\e.": yank-last-arg
"\e_": yank-last-arg

You can find the details about readline and bind -p here: http://linux.about.com/library/cmd/blcmdl3_readline.htm

And some information about Vi keyblindings in bash and Readline VI Editing Mode Cheat Sheet

3

Inside your .bashrc, add these:

set -o vi
bind -m vi-command ".":insert-last-argument
bind -m vi-insert "\C-l.":clear-screen
bind -m vi-insert "\C-a.":beginning-of-line
bind -m vi-insert "\C-e.":end-of-line
bind -m vi-insert "\C-w.":backward-kill-word

These will restore the default behaviour of not only ESC-dot - but also Ctrl-A, Ctrl-E, Ctrl-W and Ctrl-L. You can therefore enjoy the normal bash vi-mode and still use the shortcuts you know and love. If you need more actions, just check "man bash" to find the name of the readline action you need (like "clear-screen", "end-of-line", etc).

0

You can use $_ to refer to the last argument of the previous command.

The disadvantages are:

  1. You can't verify before pressing Enter that the thing inserted is the thing you intended to insert.

  2. If you use PROMPT_COMMAND or a DEBUG trap (I'm not sure which, exactly, causes the problem; I have both in my .bashrc) to do fancy stuff like put the running command into the xterm title, it will clobber $_.

2
  • I guess this answer is slightly off topic, but if you enjoy $_ but want to see what it was before running it, I use !$ and you can press tab to substitute it before running it. Works for me in zsh. Doesn't appear to work in bash, bash may use a different keybind.
    – Albert
    Commented Apr 14 at 21:05
  • FWIW I discovered a way to have PROMPT_COMMAND preserve the value of $_: all you have to do is make sure the last command in $PROMPT_COMMAND is something that ignores its argument, and then you pass "$_" to it. Commented Apr 15 at 8:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .