4

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
/usr/bin/zsh
$ echo $TERM
xterm
David
  • 167

2 Answers2

6

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”.

1

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.