1 # Flag indicating if we've previously jumped to last directory
2 typeset -g ZSH_LAST_WORKING_DIRECTORY
4 # Updates the last directory once directory is changed
5 autoload -U add-zsh-hook
6 add-zsh-hook chpwd chpwd_last_working_dir
7 chpwd_last_working_dir() {
8 # Don't run in subshells
9 [[ "$ZSH_SUBSHELL" -eq 0 ]] || return 0
10 # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
11 local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
15 # Changes directory to the last working directory
17 # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
18 local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
19 [[ -r "$cache_file" ]] && cd "$(cat "$cache_file")"
22 # Jump to last directory automatically unless:
23 # - this isn't the first time the plugin is loaded
24 # - it's not in $HOME directory
25 [[ -n "$ZSH_LAST_WORKING_DIRECTORY" ]] && return
26 [[ "$PWD" != "$HOME" ]] && return
28 lwd 2>/dev/null && ZSH_LAST_WORKING_DIRECTORY=1 || true