summaryrefslogtreecommitdiff
path: root/oh-my-zsh/plugins/last-working-dir/last-working-dir.plugin.zsh
diff options
context:
space:
mode:
authorAditya <bluenerd@protonmail.com>2023-02-27 20:04:56 +0530
committerAditya <bluenerd@protonmail.com>2023-02-27 20:04:56 +0530
commitedc449275b6c04445f58b108ca0937a87c1e8430 (patch)
tree9fd484d58145b616f29a78857cc0b1c8b1c18f05 /oh-my-zsh/plugins/last-working-dir/last-working-dir.plugin.zsh
parent6f5424ca96c4221ef433f545642669e9c104d0ed (diff)
add zsh
Diffstat (limited to 'oh-my-zsh/plugins/last-working-dir/last-working-dir.plugin.zsh')
-rw-r--r--oh-my-zsh/plugins/last-working-dir/last-working-dir.plugin.zsh28
1 files changed, 28 insertions, 0 deletions
diff --git a/oh-my-zsh/plugins/last-working-dir/last-working-dir.plugin.zsh b/oh-my-zsh/plugins/last-working-dir/last-working-dir.plugin.zsh
new file mode 100644
index 0000000..684972c
--- /dev/null
+++ b/oh-my-zsh/plugins/last-working-dir/last-working-dir.plugin.zsh
@@ -0,0 +1,28 @@
+# Flag indicating if we've previously jumped to last directory
+typeset -g ZSH_LAST_WORKING_DIRECTORY
+
+# Updates the last directory once directory is changed
+autoload -U add-zsh-hook
+add-zsh-hook chpwd chpwd_last_working_dir
+chpwd_last_working_dir() {
+ # Don't run in subshells
+ [[ "$ZSH_SUBSHELL" -eq 0 ]] || return 0
+ # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
+ local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
+ builtin pwd >| "$cache_file"
+}
+
+# Changes directory to the last working directory
+lwd() {
+ # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
+ local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
+ [[ -r "$cache_file" ]] && cd "$(cat "$cache_file")"
+}
+
+# Jump to last directory automatically unless:
+# - this isn't the first time the plugin is loaded
+# - it's not in $HOME directory
+[[ -n "$ZSH_LAST_WORKING_DIRECTORY" ]] && return
+[[ "$PWD" != "$HOME" ]] && return
+
+lwd 2>/dev/null && ZSH_LAST_WORKING_DIRECTORY=1 || true