From b26000d1685fa61bddddea42e2a606b2a0d64e25 Mon Sep 17 00:00:00 2001 From: Jhon Pedroza Date: Tue, 16 Apr 2024 02:52:12 -0500 Subject: [PATCH] feat(tmux): sessions named as current dir (#12351) --- plugins/tmux/README.md | 1 + plugins/tmux/tmux.plugin.zsh | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/plugins/tmux/README.md b/plugins/tmux/README.md index 50f8b2c2f..09952a9f5 100644 --- a/plugins/tmux/README.md +++ b/plugins/tmux/README.md @@ -37,6 +37,7 @@ The plugin also supports the following: | `ZSH_TMUX_AUTOQUIT` | Automatically closes terminal once tmux exits (default: `ZSH_TMUX_AUTOSTART`) | | `ZSH_TMUX_CONFIG` | Set the configuration path (default: `$HOME/.tmux.conf`, `$XDG_CONFIG_HOME/tmux/tmux.conf`) | | `ZSH_TMUX_DEFAULT_SESSION_NAME` | Set tmux default session name when autostart is enabled | +| `ZSH_TMUX_AUTONAME_SESSION` | Automatically name new sessions based on the basename of `$PWD` (default: `false`) | | `ZSH_TMUX_DETACHED` | Set the detached mode (default: `false`) | | `ZSH_TMUX_FIXTERM` | Sets `$TERM` to 256-color term or not based on current terminal support | | `ZSH_TMUX_FIXTERM_WITHOUT_256COLOR` | `$TERM` to use for non 256-color terminals (default: `tmux` if available, `screen` otherwise) | diff --git a/plugins/tmux/tmux.plugin.zsh b/plugins/tmux/tmux.plugin.zsh index 791592d8f..f65598358 100644 --- a/plugins/tmux/tmux.plugin.zsh +++ b/plugins/tmux/tmux.plugin.zsh @@ -13,6 +13,8 @@ fi : ${ZSH_TMUX_AUTOCONNECT:=true} # Automatically close the terminal when tmux exits : ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART} +# Automatically name the new session based on the basename of PWD +: ${ZSH_TMUX_AUTONAME_SESSION:=false} # Set term to screen or screen-256color based on current terminal support : ${ZSH_TMUX_DETACHED:=false} # Set detached mode @@ -102,9 +104,22 @@ function _zsh_tmux_plugin_run() { local _detached="" [[ "$ZSH_TMUX_DETACHED" == "true" ]] && _detached="-d" + + local session_name + if [[ "$ZSH_TMUX_AUTONAME_SESSION" == "true" ]]; then + # Name the session after the basename of the current directory + session_name=${PWD##*/} + # If the current directory is the home directory, name it 'HOME' + [[ "$PWD" == "$HOME" ]] && session_name="HOME" + # If the current directory is the root directory, name it 'ROOT' + [[ "$PWD" == "/" ]] && session_name="ROOT" + else + session_name="$ZSH_TMUX_DEFAULT_SESSION_NAME" + fi + # Try to connect to an existing session. - if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then - [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t $ZSH_TMUX_DEFAULT_SESSION_NAME + if [[ -n "$session_name" ]]; then + [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name" else [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached fi @@ -116,8 +131,9 @@ function _zsh_tmux_plugin_run() { elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then tmux_cmd+=(-f "$ZSH_TMUX_CONFIG") fi - if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then - $tmux_cmd new-session -s $ZSH_TMUX_DEFAULT_SESSION_NAME + + if [[ -n "$session_name" ]]; then + $tmux_cmd new-session -s "$session_name" else $tmux_cmd new-session fi