I (ab)use Alt + . to recover the last argument in a previous command (I'm using ZSH): for example,

$ convert img.png img.pdf
$ llpp (alt + .) # which produces llpp img.pdf

but sometimes I review a pdf with llpp

$ llpp pdffile.pdf& 

and then if I try to do something else with pdffile.pdf I run into troubles

$ llpp (`Alt` + `.`) # produces llpp &

So, is there any way to recover pdffile.pdf using something similar to Alt + .?

$ echo $SHELL
$ echo $TERM
  • 1
    What shell are you using? bash? – Kusalananda Dec 24 '18 at 19:26
  • 1
    What is the output of echo $SHELL; echo $TERM; – Timothy Pulliam Dec 24 '18 at 20:01

ESC-. (insert-last-word) considers any space-separated or space-separable shell token¹ a “word“, including punctuation tokens such as &.

You can give it a numeric argument to grab a word other than the last one. Positive arguments count from the right: Alt+1 Alt+. is equivalent to Alt+., Alt+2 Alt+. grabs the previous word, etc. Alt+0 Alt+. is the previous word, and negative arguments continue from the left, e.g. Alt+- Alt+1 Alt+. is the first argument.

I have copy-earlier-word bound to ESC-,. Where repeated invocations of ESC-. insert the last word of successive commands going back in the history, repeated invocations of ESC-, after ESC-. insert the previous word of the same command. So with the following code in your .zshrc, you can get the next-to-last word of the previous command with Alt+. Alt+,.

autoload -U copy-earlier-word
zle -N copy-earlier-word
bindkey '^[,' copy-earlier-word

¹ There are several reasonable definitions of “token” in this context. In this answer I'm going by the definition “something that insert-last-word considers to be a separate word”.


In bash, it is possible to prepend a numeric argument to the action.

So, a negative argument of -1 will address the argument before.

You type:

Alt---1 Alt-.

Or simply:

Alt-- Alt-.

I don't know how to express that in zsh.

  • Zsh has the same feature, but counts in the other direction. – Gilles Dec 25 '18 at 13:50

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.