From man zshoptions
HIST_VERIFY
Whenever the user enters a line with history expansion, don't execute the line
directly; instead, perform history expansion and reload the line into the editing buffer.
To disable this option run setopt no_hist_verify
. You can also have a look at your config file and remove the hist_verify option or just explicitly disable it.
pushd
, popd
, and dirs
are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came.
For example
start up with the following directories:
$ pwd
/home/saml/somedir
$ ls
dir1 dir2 dir3
pushd to dir1
$ pushd dir1
~/somedir/dir1 ~/somedir
$ dirs
~/somedir/dir1 ~/somedir
dirs
command confirms that we have 2 directories on the stack now. dir1
and the original dir, somedir
. NOTE: Our "current" directory is ~/somedir/dir1
.
pushd to ../dir3 (because we're inside dir1
now)
$ pushd ../dir3
~/somedir/dir3 ~/somedir/dir1 ~/somedir
$ dirs
~/somedir/dir3 ~/somedir/dir1 ~/somedir
$ pwd
/home/saml/somedir/dir3
dirs
shows we have 3 directories in the stack now. dir3
, dir1
, and somedir
. Notice the direction. Every new directory is getting added to the left. When we start popping directories off, they'll come from the left as well.
manually change directories to ../dir2
$ cd ../dir2
$ pwd
/home/saml/somedir/dir2
$ dirs
~/somedir/dir2 ~/somedir/dir1 ~/somedir
Now start popping directories
$ popd
~/somedir/dir1 ~/somedir
$ pwd
/home/saml/somedir/dir1
Notice we popped back to dir1
.
Pop again...
$ popd
~/somedir
$ pwd
/home/saml/somedir
And we're back where we started, somedir
.
Might get a little confusing, but the head of the stack is the directory that you're currently in. Hence when we get back to somedir
, even though dirs
shows this:
$ dirs
~/somedir
Our stack is in fact empty.
$ popd
bash: popd: directory stack empty
Best Answer
It depends. In
zsh
you can configurecd
to push the old directory on the directory stack automatically, but it is not the default setting.As far as I can tell
zsh
with default settings behaves very similar tobash
:cd somedir
somedir
OLDPWD
PWD="somedir"
dirs
) withsomedir
(the number of elements on the stack does not change).cd -
:$OLDPWD
PWD
andOLDPWD
PWD
pushd somedir
:somedir
OLDPWD
PWD="somedir"
somedir
onto the directory stack (extending it by one element)popd
:OLDPWD
PWD
to the new top element of the directory stackNote: Whether the present working directory is considered an element of the directory stack differs between
zsh
andbash
. I usedbash
as reference for the above lists.In
bash
the present working directory is considered to be the top element of the directory stack. Theman 1 bash
says:Printing
DIRSTACK
(echo ${dirstack[@]}
) confirms that the first element is identical to$PWD
.In
zsh
the present working directory is not part of the directory stack (but still shown withdirs
).man 1 zshbuiltins
says:Printing
dirstack
(echo ${dirstack[@]}
) and comparing it to the output ofdirs
should show that thePWD
is not part of `dirstack.In both shells
dirs
prints the present working directory as the first element. Also in both shells, the directory stack element with the index1
refers to the directory which was current before the lastpushd
. That is because arrays inzsh
are usually numbered from1
, while they are numbered from0
inbash
. So there is little practical differenceAs said above, this behavior can be modified in
zsh
.If you set the
AUTO_PUSHD
option inzsh
(setopt autopushd
)cd somedir
behaves likepushd somedir
, the previous directory is pushed onto the directory stack automatically. This is probably the case on your machine. You can runsetopt
to get a list of options that are not set the default way. See, whetherautopushd
appears in the list.But this does not modify
cd -
to behave likepopd
. Instead it just pushes$PWD
onto the directory stack, and changes directory to$OLDPWD
. That means repeatedly callingcd -
will actually grow the directory stack (($PWD $OLDPWD $PWD $OLDPWD $PWD …)
). If it actually does behave exactly likepopd
on your system, I would suggest ifcd
is actually the builtin (whence -v cd
); it is possible that its replaced with an alias or function.As the directory stack will grow rather quickly with
AUTO_PUSHD
enabled, you can limit its size by setting the parameterDIRSTACKSIZE
to the desired maximum size.You can also prevent duplicates by setting the
PUSHD_IGNORE_DUPS
option.For more options have a look at the manual.