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

Add ability to have multiple auto-connected sessions #4908

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 26 additions & 2 deletions plugins/tmux/tmux.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ if which tmux &> /dev/null
# systems without the proper terminfo
[[ -n "$ZSH_TMUX_FIXTERM_WITH_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITH_256COLOR="screen-256color"

ZSH_TMUX_SESSION_NAME_PREFIX="`whoami`-zsh-tmux-"


# Get the absolute path to the current directory
local zsh_tmux_plugin_path="$(cd "$(dirname "$0")" && pwd)"
Expand All @@ -57,17 +59,39 @@ if which tmux &> /dev/null
export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.only.conf"
fi

# helper functions
function _incr() {
var=$(($1 + 1))
echo $var
}

function _inactive_sessions {
INACTIVE_SESSIONS=`tmux ls | grep -v attached | grep "$ZSH_TMUX_SESSION_NAME_PREFIX" | cut -d: -f1`
[[ -n $INACTIVE_SESSIONS ]] && echo $INACTIVE_SESSIONS
}

function _all_sessions {
ALL_SESSIONS=`tmux ls | grep "$ZSH_TMUX_SESSION_NAME_PREFIX" | cut -d: -f1`
[[ -n $ALL_SESSIONS ]] && echo $ALL_SESSIONS
}

# Wrapper function for tmux.
function _zsh_tmux_plugin_run()
{
# We have other arguments, just run them
if [[ -n "$@" ]]
then
\tmux $@
# Try to connect to an existing session.
# Try to connect to an existing named session.
elif [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]]
then
\tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` attach || \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG` new-session
# See if we need to create a new named session
if [[ `_inactive_sessions | wc -l` != 0 ]]
then
\tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` attach -t `_inactive_sessions | head -n1`
else
\tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG` new-session -s "$ZSH_TMUX_SESSION_NAME_PREFIX`_incr $(_all_sessions | tail -n1 | cut -d- -f4)`"
fi
[[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
# Just run tmux, fixing the TERM variable if requested.
else
Expand Down