6

I have in .zshrc

 setopt autopushd

It makes cd acts like pushd. This means that we have no stack at all. When I have used it, I have not used stack at all, since it removes stack from me.

I am not sure what is the benefit of autopushd.

What is the benefit of autopushd?

2 Answers2

11

pushd is like cd, but it also pushes your current directory onto a stack. You might be in /some/deep/directory and then need to poke around in /var/www for a while:

crb@server /some/deep/directory $ pushd /var/www/
/var/www /some/deep/directory
crb@server /var/www $ [do some stuff]
crb@server /var/www $ popd
/some/deep/directory
crb@server /some/deep/directory $

autopushd means that even if you type 'cd', you get the effect of having typed 'pushd', which you don't remember you wanted to use until you think "man, I really wish I could get back to where I was before". You can then popd your way back through your directory history.

It's useful to know you can always change to the directory you were in before this one (as opposed to the parent directory) by typing 'cd -'.

Here's another good writeup.

Edit Why use pushd when you can use cd - ?

cd /foo/bar
cd /baz
cd /somewhere/else

cd - will only get you up to /baz. pushd will get you all the way back up to where you were before you typed 'cd /foo/bar'.

The directory stack is particularly useful in scripts, where you can't just go back through your command history and recognize the name of the directory you were in:

pushd /var/foo
# run a command, which might well change your PWD at the end of its execution - especially if it fails
popd

You are now guaranteed to be in /var/foo.

crb
  • 7,928
  • 37
  • 53
0

The AUTO_PUSHD option lets you use cd to get back to any directory you've ever cd'ed to. Without this option you are only able to get back to the previous directory you've cd'ed to but with this option you can use something like cd -5 to get back to the 5th directory you've visited.

Jake
  • 443
  • 1
  • 4
  • 6