Is there a way that I could reference the same path from one command to the next? For example, I may want to list the contents of a specific folder:

$ ls ~/Documents/some/dir

Then, once I've done that, I may want to perform some action in that same directory:

$ mv ~/Documents/some/dir/file.txt ~/Documents/other/dir

Is there a way to, essentially, invoke that path without typing it again (or using some sort of auto-suggestion or auto-fill that I can do with .zsh)?

I vaguely remember reading about something along these lines but I don't remember what the technique is?

  • You're right, the answer is the $_ variable (?). That is pretty much what I'm looking for although Stéphane's insert-last-word suggestion also helpful. – Paul Jacobson Dec 12 '17 at 10:56
  • Stéphane mentioned the $_ variable too ;-). The linked question also mentions the Alt-. shortcut. (When an existing question’s answers apply to a new question, we prefer linking them. It’s not supposed to reflect negatively on the new questions and answers, it’s just the way things are done here.) – Stephen Kitt Dec 12 '17 at 11:34
  • Thanks @StephenKitt, I saw that Stéphane mentioned the $_ variable (although I did miss that the first time I read his answer). I appreciate the clarification and the link to the other question. – Paul Jacobson Dec 12 '17 at 11:37

That's the insert-last-word widget which in emacs mode is bound to Alt+_ and Alt+. by default.


$ ls ~/Documents/some/dir
$ ls Alt+_/file.txt Alt+_

Or you can use the $_ special variable, or csh-style history expansion (!$) if you prefer.

  • Is that only available in Emacs? – Paul Jacobson Dec 12 '17 at 10:39
  • @PaulJacobson, in the emacs editor mode of zsh which is the default unless you have set $EDITOR or $VISUAL to vi in which case, you'd get the vi mode. – Stéphane Chazelas Dec 12 '17 at 10:41
  • Hah! I learned something new today. Thanks! – Paul Jacobson Dec 12 '17 at 10:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.