last-working-dir.plugin.zsh 739 B

1234567891011121314151617181920212223
  1. # Flag indicating if we've previously jumped to last directory
  2. typeset -g ZSH_LAST_WORKING_DIRECTORY
  3. # Updates the last directory once directory is changed
  4. chpwd_functions+=(chpwd_last_working_dir)
  5. chpwd_last_working_dir() {
  6. local cache_file="$ZSH_CACHE_DIR/last-working-dir"
  7. pwd >| "$cache_file"
  8. }
  9. # Changes directory to the last working directory
  10. lwd() {
  11. local cache_file="$ZSH_CACHE_DIR/last-working-dir"
  12. [[ -r "$cache_file" ]] && cd "$(cat "$cache_file")"
  13. }
  14. # Jump to last directory automatically unless:
  15. # - this isn't the first time the plugin is loaded
  16. # - it's not in $HOME directory
  17. [[ -n "$ZSH_LAST_WORKING_DIRECTORY" ]] && return
  18. [[ "$PWD" != "$HOME" ]] && return
  19. lwd 2>/dev/null && ZSH_LAST_WORKING_DIRECTORY=1 || true