Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: persistant Directory history?

: On Wed, Nov 08, 2006 at 01:49AM +0530, chesss wrote:

> Finally this worked for me  just perfect :)  . i got it from
> http://www.zsh.org/mla/users/2006/msg00173.html

That was my first attempt (i.e. taking code from someone else ;-)) to
solve it, but there were problems with it.

> if [[ -f ~/.zdirs ]] && [[ ${#dirstack[*]} -eq 0 ]]; then
> dirstack=( $(< ~/.zdirs) )
> popd > /dev/null
> fi
> precmd() {
> dirs -l >! ~/.zdirs # added -l
> }
> cd
> The only problem with the above was that a new shell would start with
> the last directory. So I added the extra 'cd' at the end :D

Here are the caveats of that code:

1. it will fail if your directories have spaces in their names. To solve
   this you have to use "dirs -p" to put one dir per line, and use "f"
   to read them.

2. The popd is used to avoid having multiple repeated entries; but a
   beter solution is to simply use a "u" when loading the directories so
   that duplicate entries are deleted.

3. You dont need to save the dirstack after every command, only when you
   change dirs, so it's beter to use "chpwd" instead of "precmd".

Making these changes, you would have a better ;-) persistent dirstack
history with:

if [[ -f ~/.zdirs ]] && [[ ${#dirstack[*]} -eq 0 ]]; then
    dirstack=( ${(uf)"$(< ~/.zdirs)"} )
chpwd() { dirs -pl >! ~/.zdirs }

If you want $OLDPWD to work as well, then you have to include

cd $dirstack[0] && cd - > /dev/null

inside that "if" block.

Francisco Borges

Messages sorted by: Reverse Date, Date, Thread, Author