The Arch Wiki has this interesting bit of zsh configuration:
DIRSTACKFILE="$HOME/.cache/zsh/dirs"
if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]]; then
dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
[[ -d $dirstack[1] ]] && cd $dirstack[1]
fi
chpwd() {
print -l $PWD ${(u)dirstack} >$DIRSTACKFILE
}
DIRSTACKSIZE=20
setopt autopushd pushdsilent pushdtohome
## Remove duplicate entries
setopt pushdignoredups
## This reverts the +/- operators.
setopt pushdminus
Even though pushdignoredups
is set, I find duplicate entries in the output of dirs
, sometimes even the same entry repeated thrice. The Wiki does go on to say that this doesn't work well with multiple zsh sessions, and I do have multiple terminal tabs open with zsh running, so that may be the cause of the problem.
Can I modify this function somehow to keep only unique entries? Is this a convoluted way of achieving something that zsh provides easier ways to do?
How can I have a persistent dirstack, with only unique entries?