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?

10

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: I still cannot see its benefit. --- I use always % cd - % to go back to the folder. --- I use % cd % to go to HOME. – Léo Léopold Hertz 준영 Jul 2 '09 at 21:57
  • @crb: I now understand what you mean. It seems to be useful. --- You have to remember % popd % to get back where you were. – Léo Léopold Hertz 준영 Jul 2 '09 at 22:00
  • @crb: I remember that there are also some list which shows you your current directories. --- I was able to use it by writing % jobs; popd <number> % However, I could not see the stack by jobs. – Léo Léopold Hertz 준영 Jul 2 '09 at 22:02
  • 'dirs' - see acm.uiuc.edu/workshops/zsh/dir_stack.html. – crb Jul 2 '09 at 22:06
  • @crb: Thank you for your answers! --- I now understand the benefit of popd. --- So to benefit most I need to rememeber % popd %, and to benefit slightly more, I need to remember % dirs % which I had forgot. – Léo Léopold Hertz 준영 Jul 2 '09 at 22:16

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.