Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tmux sessiongroup support -- Make ZSH_TMUX_AUTOCONNECT better on multiple concurrent terminals #12032

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/tmux/README.md
Expand Up @@ -34,6 +34,7 @@ The plugin also supports the following:
| `ZSH_TMUX_AUTOSTART` | Automatically starts tmux (default: `false`) |
| `ZSH_TMUX_AUTOSTART_ONCE` | Autostart only if tmux hasn't been started previously (default: `true`) |
| `ZSH_TMUX_AUTOCONNECT` | Automatically connect to a previous session if it exits (default: `true`) |
| `ZSH_TMUX_SESSIONGROUP` | With `ZSH_TMUX_AUTOCONNECT` specified, connect to existing session group |
| `ZSH_TMUX_AUTOQUIT` | Automatically closes terminal once tmux exits (default: `ZSH_TMUX_AUTOSTART`) |
| `ZSH_TMUX_FIXTERM` | Sets `$TERM` to 256-color term or not based on current terminal support |
| `ZSH_TMUX_ITERM2` | Sets the `-CC` option for iTerm2 tmux integration (default: `false`) |
Expand Down
27 changes: 23 additions & 4 deletions plugins/tmux/tmux.plugin.zsh
Expand Up @@ -12,6 +12,8 @@ fi
# Automatically connect to a previous session if it exists
: ${ZSH_TMUX_AUTOCONNECT:=true}
# Automatically close the terminal when tmux exits
: ${ZSH_TMUX_SESSIONGROUP:=true}
# With ZSH_TMUX_AUTOCONNECT specified, connect to existing session group
: ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
# Set term to screen or screen-256color based on current terminal support
: ${ZSH_TMUX_FIXTERM:=true}
Expand Down Expand Up @@ -78,10 +80,27 @@ function _zsh_tmux_plugin_run() {
[[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)

# Try to connect to an existing session.
if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach -t $ZSH_TMUX_DEFAULT_SESSION_NAME
else
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
if [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]]; then
if [[ "$ZSH_TMUX_SESSIONGROUP" == "true" ]]; then
# Instead of attaching, we can fairly freely run new-session which will
# give us a new session grouped with the specified target session.
# Adding `set-option destroy-unattached` discourages grouped sessions
# from accumulating over time
if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
$tmux_cmd new-session -t $ZSH_TMUX_DEFAULT_SESSION_NAME \; set-option destroy-unattached
else
# The session group of 0 is a very rough guess. Probably better would be
# to grab it from `tmux ls -F '#{session_name}' | tail -n 1` and bail if
# there is none.
$tmux_cmd new-session -t 0 \; set-option destroy-unattached
fi
else
if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
$tmux_cmd attach -t $ZSH_TMUX_DEFAULT_SESSION_NAME
else
$tmux_cmd attach
fi
fi
fi

# If failed, just run tmux, fixing the TERM variable if requested.
Expand Down