b26000d1685fa61bddddea42e2a606b2a0d64e25

Author
Jhon Pedroza <jhon@pedroza.me>
Committer
GitHub <noreply@github.com>
Date

Message

feat(tmux): sessions named as current dir (#12351)

Diff

 1diff --git a/plugins/tmux/README.md b/plugins/tmux/README.md
 2index 50f8b2c2f21a0cd48d2d4a739503a4219eb36316..09952a9f5ac5c862472bf34c5d6effccadf47901 100644
 3--- a/plugins/tmux/README.md
 4+++ b/plugins/tmux/README.md
 5@@ -37,6 +37,7 @@ The plugin also supports the following:
 6 | `ZSH_TMUX_AUTOQUIT`                 | Automatically closes terminal once tmux exits (default: `ZSH_TMUX_AUTOSTART`)                               |
 7 | `ZSH_TMUX_CONFIG`                   | Set the configuration path (default: `$HOME/.tmux.conf`, `$XDG_CONFIG_HOME/tmux/tmux.conf`)                 |
 8 | `ZSH_TMUX_DEFAULT_SESSION_NAME`     | Set tmux default session name when autostart is enabled                                                     |
 9+| `ZSH_TMUX_AUTONAME_SESSION`         | Automatically name new sessions based on the basename of `$PWD` (default: `false`)                          |
10 | `ZSH_TMUX_DETACHED`                 | Set the detached mode (default: `false`)                                                                    |
11 | `ZSH_TMUX_FIXTERM`                  | Sets `$TERM` to 256-color term or not based on current terminal support                                     |
12 | `ZSH_TMUX_FIXTERM_WITHOUT_256COLOR` | `$TERM` to use for non 256-color terminals (default: `tmux` if available, `screen` otherwise)               |
13diff --git a/plugins/tmux/tmux.plugin.zsh b/plugins/tmux/tmux.plugin.zsh
14index 791592d8f6d41c045391ea9b225413bfd86517bd..f655983581697f2c0c1ab5ec8fad9673fdbf1e09 100644
15--- a/plugins/tmux/tmux.plugin.zsh
16+++ b/plugins/tmux/tmux.plugin.zsh
17@@ -13,6 +13,8 @@ fi
18 : ${ZSH_TMUX_AUTOCONNECT:=true}
19 # Automatically close the terminal when tmux exits
20 : ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
21+# Automatically name the new session based on the basename of PWD
22+: ${ZSH_TMUX_AUTONAME_SESSION:=false}
23 # Set term to screen or screen-256color based on current terminal support
24 : ${ZSH_TMUX_DETACHED:=false}
25 # Set detached mode
26@@ -102,9 +104,22 @@ function _zsh_tmux_plugin_run() {
27 
28   local _detached=""
29   [[ "$ZSH_TMUX_DETACHED" == "true" ]] && _detached="-d"
30+
31+  local session_name
32+  if [[ "$ZSH_TMUX_AUTONAME_SESSION" == "true" ]]; then
33+    # Name the session after the basename of the current directory
34+    session_name=${PWD##*/}
35+    # If the current directory is the home directory, name it 'HOME'
36+    [[ "$PWD" == "$HOME" ]] && session_name="HOME"
37+    # If the current directory is the root directory, name it 'ROOT'
38+    [[ "$PWD" == "/" ]] && session_name="ROOT"
39+  else
40+      session_name="$ZSH_TMUX_DEFAULT_SESSION_NAME"
41+  fi
42+
43   # Try to connect to an existing session.
44-  if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
45-    [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t $ZSH_TMUX_DEFAULT_SESSION_NAME
46+  if [[ -n "$session_name" ]]; then
47+    [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name"
48   else
49     [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached
50   fi
51@@ -116,8 +131,9 @@ function _zsh_tmux_plugin_run() {
52     elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
53       tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
54     fi
55-    if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
56-      $tmux_cmd new-session -s $ZSH_TMUX_DEFAULT_SESSION_NAME
57+
58+    if [[ -n "$session_name" ]]; then
59+      $tmux_cmd new-session -s "$session_name"
60     else
61       $tmux_cmd new-session
62     fi