Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Merged saved stack' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Merged saved stack
Authored by: jdsmith on Dec 07, '11 01:20:34PM
I came up with a solution that merges together the directory stacks of parallel sessions, writing the merged list on logout. If you regularly have multiple terminals open on the same machine, this ensures that all the directory history you accumulate is saved (and sensibly interleaved). Since there is no date-stamp for a proper history, I put at the end of the list any common "tail" less-recently visited directories.

Place this in ~/.zprofile (to load the saved dirstack):


# Load the saved Zdirs Stack
if [ -f ~/.zdirs -a ${#dirstack} -eq 0 ]; then
    dirstack=( ${(f)"$(< ~/.zdirs)"} )
    popd > /dev/null
fi
And put this in ~/.zlogout (to merge and save the stack on logout):

set -A tdirs ${(f)"$(dirs -pl)"} # Current directory stack
if [ -e ~/.zdirs ]; then
    set -A zdirs ${(f)"$(<~/.zdirs)"} # Saved stack
    # Find matching list tails
    for i in {1..$#zdirs}; do
	[ \! -z ${(M)${(F)tdirs}%${(F)zdirs[$i,-1]}} ] && {
	    (( tail_length=$#zdirs-$i+1 )); # This length tail matches
	    break; 
	}
    done 
    # Trim the matched tail off tdirs
    set -A tdirs $tdirs[1,-(( $tail_length+1 ))] $zdirs
fi

print -l ${(u)tdirs} >! ~/.zdirs   


[ Reply to This | # ]