In zsh, the key combination Alt+. is bound to insert-last-word, and it will insert the last argument from previous command on the current command line.

I am looking for a keybinding / shortcut that will act similar to Alt+., except that it will paste all the arguments from previous command on the command line .

I know I can type !* and zsh will interpret it as "reuse all arguments from previous command". But this is not quite what I am looking for. Also, it does not actually paste the arguments so that I can see them, zsh only interprets !* as such. I could hit Tab to have it expanded, but that is yet another command necessary.

I would prefer to have this as a key combination such as Alt+something, instead of having to type '!*' and hitting tab

How can I do this ?

  • Note that !* expands to all but the first token from the previous history entry, not all arguments from previous command. If the previous entry was (echo foo; echo bar), !* expands to echo foo; echo bar) not bar. Is that what you want? – Stéphane Chazelas Aug 26 at 15:08

To insert all but the first word of the previous history entry, you could define a custom widget like:

insert-last-words() {
  emulate -L zsh
  set -o extendedglob

  if [[ $WIDGET = $LASTWIDGET ]]; then
    # subsequent invocations go further back in history like
    # insert-last-word

    zle undo # previous invocation
    ((INSERT_LAST_WORDS_INDEX += ${1--1}))

  local lastwords
  local cmd=${history:$INSERT_LAST_WORDS_INDEX:1}
  (($#lastwords > 1)) || return


zle -N insert-last-words
bindkey '\e,' insert-last-words

(here bound to Alt+,).

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.