Diff
1diff --git a/plugins/last-working-dir/README.md b/plugins/last-working-dir/README.md
2index ced9e0370d315b426493a5269d08e4b904a191d8..b5cc55828096751126b2430bcd02fd3e595e226f 100644
3--- a/plugins/last-working-dir/README.md
4+++ b/plugins/last-working-dir/README.md
5@@ -1,15 +1,33 @@
6 # last-working-dir plugin
7
8 Keeps track of the last used working directory and automatically jumps into it
9-for new shells, unless:
10+for new shells, unless the starting directory is not `$HOME`.
11
12-- The plugin is already loaded.
13-- The current `$PWD` is not `$HOME`.
14-
15-Also adds `lwd` function to jump to the last working directory.
16+Also adds a `lwd` function to jump to the last working directory.
17
18 To use it, add `last-working-dir` to the plugins array in your zshrc file:
19
20 ```zsh
21 plugins=(... last-working-dir)
22 ```
23+
24+## Features
25+
26+### Use separate last-working-dir files with different SSH keys
27+
28+If the same user account is used by multiple users connecting via different SSH keys, you can
29+configure SSH to map them to different `SSH_USER`s and the plugin will use separate lwd files
30+for each one.
31+
32+Make sure that your SSH server allows environment variables. You can enable this feature
33+within the `/etc/sshd/sshd_config` file:
34+
35+```
36+PermitUserEnvironment yes
37+```
38+
39+Then, add `environment="SSH_USER=<SSH_USERNAME>"` before the SSH keys in your `authorized_keys` file:
40+
41+```
42+environment="SSH_USER=a.test@example.com" ssh-ed25519 AAAAC3Nz...
43+```
44diff --git a/plugins/last-working-dir/last-working-dir.plugin.zsh b/plugins/last-working-dir/last-working-dir.plugin.zsh
45index fd21705ae2ccdd4a20ab93e639d4aefa4d5a83ca..905a02a702cc0773a2cb3737007c538a7d9d1014 100644
46--- a/plugins/last-working-dir/last-working-dir.plugin.zsh
47+++ b/plugins/last-working-dir/last-working-dir.plugin.zsh
48@@ -5,16 +5,18 @@ typeset -g ZSH_LAST_WORKING_DIRECTORY
49 autoload -U add-zsh-hook
50 add-zsh-hook chpwd chpwd_last_working_dir
51 chpwd_last_working_dir() {
52- if [ "$ZSH_SUBSHELL" = 0 ]; then
53- local cache_file="$ZSH_CACHE_DIR/last-working-dir"
54- pwd >| "$cache_file"
55- fi
56+ # Don't run in subshells
57+ [[ "$ZSH_SUBSHELL" -eq 0 ]] || return 0
58+ # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
59+ local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
60+ pwd >| "$cache_file"
61 }
62
63 # Changes directory to the last working directory
64 lwd() {
65- local cache_file="$ZSH_CACHE_DIR/last-working-dir"
66- [[ -r "$cache_file" ]] && cd "$(cat "$cache_file")"
67+ # Add ".$SSH_USER" suffix to cache file if $SSH_USER is set and non-empty
68+ local cache_file="$ZSH_CACHE_DIR/last-working-dir${SSH_USER:+.$SSH_USER}"
69+ [[ -r "$cache_file" ]] && cd "$(cat "$cache_file")"
70 }
71
72 # Jump to last directory automatically unless: