2

Is there any way to get a list of the most recent 'places' (directories) have been navigated in terminal (osx)? I mean, I'd like to open my terminal, tipe some command, get the last 2 or 3 places I've been, and go there.

Something like what the arrows do for recent commands.

Is it possible?

4
  • Use your shell's command line history feature and search for cd commands? Ctrl+R in some shells. – Kusalananda Jan 9 at 9:43
  • Mmm thank you but not exactly what I intended – Luca Reghellin Jan 9 at 9:46
  • Well, that (using the shell's history) was literally what you suggested yourself when you mentioned the arrow keys. You could also manage a set of symbolic links to directories to which you often go. The shell is not quite like a web browser, it does not have a "bookmark" facility. You may possibly find something like that in a file manager though. – Kusalananda Jan 9 at 10:05
  • pushd and popd might be what you are looking for. Instead of using cd, try using pushd. That cds to that directory, but saves the previous directory on the stack. When you popd, you go back to the previous directory. Stephane's answer is good if you already know that pushd and popd exist but want to simplify it a bit. – Stewart Jan 9 at 10:43
4

If your shell is zsh (which AFAIK is the default in newer versions of macos), you can set the autopushd option (set -o autopushd in your ~/.zshrc) which will cause zsh to remember all the places (current working directories, changed with cd/pushd/popd) you've been.

Then, you can see that stack with:

dirs

Or dirs -v, so see the index of each directory on the stack, which you can use for instance in cd +5, or ls -ld ~+5 for the 5th entry (passed the top line which is the current directory in dirs's output).

or:

print -rC1 $dirstack

($dirstack exposing that stack in an array, without the current working directory).

Or, if you're enabled advanced completion with descriptions (see compinstall) with the completion list on:

cd +<Tab>

or

cd -<Tab>

Or for any command after the ~- or ~+:

any cmd ~+<Tab>

For example, with

set -o autopushd
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' menu select=2
autoload -Uz compinit
compinit -i

in ~/.zshrc, after I've visited a few directories, upon pressing Tab twice, I get a menu such as:

% cd +1
Completing directory stack
[1 -- ~/Videos]
 2 -- ~/Downloads
 3 -- ~/
 4 -- /usr
 5 -- /tmp

Which I can navigate with the arrow keys.

The directory stack feature itself is not zsh-specific. It and the pushd / popd / dirs commands to manipulate it were introduced by csh in 1980, copied by bash (1.02 in 1989) and zsh (1.0 in 1990) later. The cd +5, cd -5 also come from csh (added to zsh in 2.0 in 1991). ~+ / ~- are from bash (1989) copied later by zsh (1991) while ~+5 is from zsh (1996), copied later by bash (1997). Only autopushd (for cd to also use that stack, like a silent pushd) is zsh-specific, and its completion framework is far ahead that of bash and now also tcsh (but is being caught up by that of fish).

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.