summaryrefslogtreecommitdiff
path: root/oh-my-zsh/plugins/last-working-dir
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
parent6f5424ca96c4221ef433f545642669e9c104d0ed (diff)
add zsh
Diffstat (limited to 'oh-my-zsh/plugins/last-working-dir')
-rw-r--r--oh-my-zsh/plugins/last-working-dir/README.md33
-rw-r--r--oh-my-zsh/plugins/last-working-dir/last-working-dir.plugin.zsh28
2 files changed, 61 insertions, 0 deletions
diff --git a/oh-my-zsh/plugins/last-working-dir/README.md b/oh-my-zsh/plugins/last-working-dir/README.md
new file mode 100644
index 0000000..b5cc558
--- /dev/null
+++ b/oh-my-zsh/plugins/last-working-dir/README.md
@@ -0,0 +1,33 @@
+# last-working-dir plugin
+
+Keeps track of the last used working directory and automatically jumps into it
+for new shells, unless the starting directory is not `$HOME`.
+
+Also adds a `lwd` function to jump to the last working directory.
+
+To use it, add `last-working-dir` to the plugins array in your zshrc file:
+
+```zsh
+plugins=(... last-working-dir)
+```
+
+## Features
+
+### Use separate last-working-dir files with different SSH keys
+
+If the same user account is used by multiple users connecting via different SSH keys, you can
+configure SSH to map them to different `SSH_USER`s and the plugin will use separate lwd files
+for each one.
+
+Make sure that your SSH server allows environment variables. You can enable this feature
+within the `/etc/sshd/sshd_config` file:
+
+```
+PermitUserEnvironment yes
+```
+
+Then, add `environment="SSH_USER=<SSH_USERNAME>"` before the SSH keys in your `authorized_keys` file:
+
+```
+environment="SSH_USER=a.test@example.com" ssh-ed25519 AAAAC3Nz...
+```
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