2

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?

2 Answers 2

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                        
5
  • After some use I'm realizing that ESC will bring the cursor back, requiring me to enter two blanks before hitting ESC _ or Alt _. Is there a better way to do this? Jan 4 at 21:07
  • 1
    @young_souvlaki 1. Curse the author of vi for having concepts of cursor position in insert mode and in command mode. 2. See my edit. Jan 4 at 21:16
  • Lol, I just realized that the post I linked to both times, was answered by you both times! Has this behavior never bitten you? What is your workaround? Jan 5 at 2:23
  • @young_souvlaki I mostly use Emacs. Jan 5 at 10:09
  • Lol nice! Vanilla or what? Jan 5 at 21:52
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.