my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
1.0 KiB

3 years ago
  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. autoload -U add-zsh-hook
  5. add-zsh-hook chpwd chpwd_last_working_dir
  6. chpwd_last_working_dir() {
  7. # Don't run in subshells
  8. [[ "$ZSH_SUBSHELL" -eq 0 ]] || return 0
  9. # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
  10. local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
  11. pwd >| "$cache_file"
  12. }
  13. # Changes directory to the last working directory
  14. lwd() {
  15. # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
  16. local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
  17. [[ -r "$cache_file" ]] && cd "$(cat "$cache_file")"
  18. }
  19. # Jump to last directory automatically unless:
  20. # - this isn't the first time the plugin is loaded
  21. # - it's not in $HOME directory
  22. [[ -n "$ZSH_LAST_WORKING_DIRECTORY" ]] && return
  23. [[ "$PWD" != "$HOME" ]] && return
  24. lwd 2>/dev/null && ZSH_LAST_WORKING_DIRECTORY=1 || true