104041a018dc9a3eb74f93815192dba4c9386e1a

Author
Peat Bakke <peat@peat.org>
Committer
GitHub <noreply@github.com>
Date

Message

feat(tmux): add `tds` alias for directory sessions (#11987)

Diff

 1diff --git a/plugins/tmux/README.md b/plugins/tmux/README.md
 2index 0f8473daf45c6fb0d0320349a5f8337a4e7db358..b4516ef2657a4d365abb64cbea950a7443dd511d 100644
 3--- a/plugins/tmux/README.md
 4+++ b/plugins/tmux/README.md
 5@@ -25,6 +25,7 @@ The plugin also supports the following:
 6 | `tkss`     | tmux kill-session -t       | Terminate named running tmux session                     |
 7 | `tmux`     | `_zsh_tmux_plugin_run`     | Start a new tmux session                                 |
 8 | `tmuxconf` | `$EDITOR $ZSH_TMUX_CONFIG` | Open .tmux.conf file with an editor                      |
 9+| `tds`      | `_tmux_directory_session`  | Creates or attaches to a session for the current path    |
10 
11 ## Configuration Variables
12 
13diff --git a/plugins/tmux/tmux.plugin.zsh b/plugins/tmux/tmux.plugin.zsh
14index 6ed91c44791d769d8d82facb4fa6a101e0b5427b..72cdd481830b799c723904d73ae95c79e6d89dcd 100644
15--- a/plugins/tmux/tmux.plugin.zsh
16+++ b/plugins/tmux/tmux.plugin.zsh
17@@ -108,6 +108,19 @@ compdef _tmux _zsh_tmux_plugin_run
18 # Alias tmux to our wrapper function.
19 alias tmux=_zsh_tmux_plugin_run
20 
21+function _tmux_directory_session() {
22+  # current directory without leading path
23+  local dir=${PWD##*/}
24+  # md5 hash for the full working directory path
25+  local md5=$(printf '%s' "$PWD" | md5sum | cut -d  ' ' -f 1)
26+  # human friendly unique session name for this directory
27+  local session_name="${dir}-${md5:0:6}"
28+  # create or attach to the session
29+  tmux new -As "$session_name"
30+}
31+
32+alias tds=_tmux_directory_session
33+
34 # Autostart if not already in tmux and enabled.
35 if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" ]]; then
36   # Actually don't autostart if we already did and multiple autostarts are disabled.