3

I am using zsh with bindkeys -v.

Alt + . does not work as expected. It seems to repeat what is currently typed in stdin, but not entered, on the next line.

This post seems to imply it does work as it does in bash, which is to grab the last argument to the last command entered.

What is needed to make this work as intended?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
young_souvlaki
  • 657
  • 1
  • 6
  • 15

2 Answers2

4

On a terminal, Alt+char is normally the same as Esc char. (It's possible to configure some terminals differently.)

In vi insert mode, Esc switches to command mode. In vi command mode, Esc does nothing. In vi command mode, . repeats the last command.

The widget insert-last-word is bound to Alt+. and Alt+_ by default in emacs mode, but it doesn't have a default binding in vi mode. If you want to use it in vi mode, you need to give it a binding, e.g.

bindkey -M vicmd _ insert-last-word

Note that this is an insert command: it inserts the text before the cursor, which can't be done at the end of a line. This is rather inconvenient for a command that's very often used at the end of a line. You may prefer to define append-last-word instead.

function append-last-word { ((++CURSOR)); zle insert-last-word; }
zle -N append-last-word
bindkey -M vicmd _ append-last-word                        
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
0

Please see this answer. https://stackoverflow.com/a/34293570/340947 Contrary to the other answer, its vicmd mapping has serious problems in my (non-comprehensive) testing! this viins mapping seems to be the trick.

Quoted below (for ~/.zshrc):

bindkey -M viins '\e.' insert-last-word

Similarly for bash, in the ~/.inputrc:

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

OK, so that covers the most common use case which is Alt+. in insert mode in these shells, but sometimes we are navigating in normal mode and want to insert last arg from before there. That's when the other answer by @gilles comes in. Note that only the basic one works to keep the cursor in the right place. Yeah it inserts before the cursor, and there seems to be no way around that.

Steven Lu
  • 2,175
  • 3
  • 24
  • 37