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?

2 Answers 2

5

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                        
2
  • 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?
    – mcp
    Jan 4, 2022 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, 2022 at 21:16
2

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.

3
  • 1
    This seems to have worked for me.. thanks
    – Eddy Ekofo
    Sep 26, 2023 at 9:54
  • 2
    I went for like 10 years before learning about alt+. It's really really handy. I still have some work to do to make it possible to go back (undo) though.
    – Steven Lu
    Sep 30, 2023 at 1:56
  • I’ve used it for the past 10 years but I have been using fish shell it worked fine with their vi-mode but at my work you can only have zsh/bash so I had to make it work somehow
    – Eddy Ekofo
    Oct 1, 2023 at 16:09

You must log in to answer this question.

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