1

I recently switched from bash to zsh. When typing a new command in bash (in vi mode), if I type Esc-_ (escape underscore), it inserts the last argument of the previous command. I want to do the same thing in zsh. (Currently, it just beeps at me.) How can I get what I want?

0

ESC-_ is more of an emacs-style key binding, but if you want to add it as well to the vi-style insert mode, you can do:

bindkey -M viins '\e_' insert-last-word

(best in ~/.zshrc).

If you also want _ in cmd mode to insert the last word after the cursor and switch to insert mode like bash and ksh93 seem to be doing (the undocumented vi-yank-arg widget in bash), you could do:

vi-yank-arg() {
  NUMERIC=1 zle .vi-add-next
  zle .insert-last-word
}
zle -N vi-yank-arg
bindkey -M vicmd _ vi-yank-arg

Which like in emacs mode would append the $NUMERICth last word from the previous command (bash and ksh93 seem to ignore the numeric argument).

10
  • Actually, it's just _. The ESC is switching to vi command mode. Which means that vicmd is the correct map. – JdeBP Jan 15 '20 at 19:18
  • That's almost right. When I add that, it deletes any trailing space (even if I typed one explicitly) and adds the last word. So if I did echo foo followed by echo <Esc>-_ it becomes echofoo with no space. How can I get it either to leave existing whitespace alone or, if none, add 1 space? – Paul J. Lucas Jan 15 '20 at 19:31
  • @JdeBP, presumably the OP wants to use ESC-_ in insert mode and remain in insert word to emulate an emacs-style behaviour like in bash/ksh – Stéphane Chazelas Jan 15 '20 at 19:47
  • @PaulJ.Lucas, I can't reproduce here with zsh 5.7.1. If you can't reproduce it either under zsh -f, then you might have other customisation that conflicts with that. – Stéphane Chazelas Jan 15 '20 at 19:50
  • @JdeBP, I see now that bash's and ksh93's _ (but not mksh's) in cmd mode inserts the last word after the cursor and goes into insert mode – Stéphane Chazelas Jan 15 '20 at 19:54

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.