last-working-dir.plugin.zsh 718 B

1234567891011121314151617181920212223
  1. #!/usr/bin/env zsh
  2. # Keeps track of the last used working directory and automatically jumps
  3. # into it for new shells.
  4. # Flag indicating if we've previously jumped to last directory.
  5. typeset -g ZSH_LAST_WORKING_DIRECTORY
  6. local cache_file="$ZSH/cache/last-working-dir"
  7. # Updates the last directory once directory is changed.
  8. function chpwd() {
  9. echo "$PWD" > "$cache_file"
  10. }
  11. # Changes directory to the last working directory.
  12. function lwd() {
  13. [[ ! -r "$cache_file" ]] || cd `cat "$cache_file"`
  14. }
  15. # Automatically jump to last working directory unless this isn't the first time
  16. # this plugin has been loaded.
  17. if [[ -z "$ZSH_LAST_WORKING_DIRECTORY" ]]; then
  18. lwd 2>/dev/null && ZSH_LAST_WORKING_DIRECTORY=1 || true
  19. fi