fix(last-working-dir): save working directory more strictly (#11343)
Fixes #11343
Diff
1diff --git a/plugins/last-working-dir/last-working-dir.plugin.zsh b/plugins/last-working-dir/last-working-dir.plugin.zsh
2index 684972cc18475d6197006e9b05c1ef1b18501c66..b816f6a8f7b4e711bb7381db8753bddbdd249b64 100644
3--- a/plugins/last-working-dir/last-working-dir.plugin.zsh
4+++ b/plugins/last-working-dir/last-working-dir.plugin.zsh
5@@ -9,20 +9,23 @@ chpwd_last_working_dir() {
6 [[ "$ZSH_SUBSHELL" -eq 0 ]] || return 0
7 # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
8 local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
9- builtin pwd >| "$cache_file"
10+ 'builtin' 'echo' '-E' "$PWD" >| "$cache_file"
11 }
1213 # 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+ [[ -r "$cache_file" ]] && cd "$(<"$cache_file")"
19 }
2021 # Jump to last directory automatically unless:
22-# - this isn't the first time the plugin is loaded
23-# - it's not in $HOME directory
24-[[ -n "$ZSH_LAST_WORKING_DIRECTORY" ]] && return
25-[[ "$PWD" != "$HOME" ]] && return
26+#
27+# - This isn't the first time the plugin is loaded
28+# - We're not in the $HOME directory (e.g. if terminal opened a different folder)
29+[[ -z "$ZSH_LAST_WORKING_DIRECTORY" ]] || return
30+[[ "$PWD" == "$HOME" ]] || return
3132-lwd 2>/dev/null && ZSH_LAST_WORKING_DIRECTORY=1 || true
33+if lwd 2>/dev/null; then
34+ ZSH_LAST_WORKING_DIRECTORY=1
35+fi