Here's a zsh script Eden Berger and I wrote to do this.
If any unused (no clients attached) tmux sessions exist, then tmux will attach to one of them (the first, sorted by their session id).
Otherwise, a new tmux session will start.
It also supports skipping tmux altogether if already in a tmux session or if SKIP_TMUX
is set.
I find the latter useful because this snippet is at the top of my .zshrc
.
So, to run my terminal without starting or attaching a tmux session, I can run an occasional SKIP_TMUX=1 alacritty
.
#!/usr/bin/env zsh
if [ -z $TMUX ] && [ -z $SKIP_TMUX ]; then
delimiter=","
sessions=$(tmux list-sessions -F "#{session_attached}${delimiter}#{session_id}")
unused_sessions=$(echo $sessions | grep ^0)
unused_sessions_ids=$(echo $unused_sessions | cut --delimiter=$delimiter --fields=2)
sorted_unused_sessions_ids=$(echo $unused_sessions_ids | sort --numeric)
first_unused_session_id=$(echo $sorted_unused_sessions_ids | head --lines 1)
if [ -z $first_unused_session_id ]; then
exec tmux new-session
else
exec tmux attach-session -t $first_unused_session_id
fi
fi