dirpersist.plugin.zsh 696 B

123456789101112131415161718192021
  1. # Save dirstack history to .zdirs
  2. # adapted from:
  3. # github.com/grml/grml-etc-core/blob/master/etc/zsh/zshrc#L1547
  4. DIRSTACKSIZE=${DIRSTACKSIZE:-20}
  5. dirstack_file=${dirstack_file:-${HOME}/.zdirs}
  6. if [[ -f ${dirstack_file} ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then
  7. dirstack=( ${(f)"$(< $dirstack_file)"} )
  8. # "cd -" won't work after login by just setting $OLDPWD, so
  9. [[ -d $dirstack[1] ]] && cd $dirstack[1] && cd $OLDPWD
  10. fi
  11. autoload -U add-zsh-hook
  12. add-zsh-hook chpwd chpwd_dirpersist
  13. chpwd_dirpersist() {
  14. if (( $DIRSTACKSIZE <= 0 )) || [[ -z $dirstack_file ]]; then return; fi
  15. local -ax my_stack
  16. my_stack=( ${PWD} ${dirstack} )
  17. builtin print -l ${(u)my_stack} >! ${dirstack_file}
  18. }