last-working-dir.plugin.zsh 740 B

123456789101112131415161718192021222324
  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. mkdir -p "$ZSH/cache"
  7. local cache_file="$ZSH/cache/last-working-dir"
  8. # Updates the last directory once directory is changed.
  9. function chpwd() {
  10. echo "$PWD" > "$cache_file"
  11. }
  12. # Changes directory to the last working directory.
  13. function lwd() {
  14. [[ ! -r "$cache_file" ]] || cd `cat "$cache_file"`
  15. }
  16. # Automatically jump to last working directory unless this isn't the first time
  17. # this plugin has been loaded.
  18. if [[ -z "$ZSH_LAST_WORKING_DIRECTORY" ]]; then
  19. lwd 2>/dev/null && ZSH_LAST_WORKING_DIRECTORY=1 || true
  20. fi