Skip to content

Instantly share code, notes, and snippets.

@ronshapiro
Created December 19, 2012 01:06
Show Gist options
  • Save ronshapiro/4333566 to your computer and use it in GitHub Desktop.
Save ronshapiro/4333566 to your computer and use it in GitHub Desktop.
A snippet to make sure you are always running a tmux session in your terminal. If you have an detached session, this will attach to it, otherwise, if there are no sessions or only ones which are attached, a new session will be started. Insert into your .bashrc or .bash_profile at the top to call every time you open a terminal session.
#############################################
# Attach tmux to the first detached session
#############################################
if [[ -z $TMUX ]]; then
TMUX_SESSIONS=`tmux list-sessions -F "#{session_name}ABC#{?session_attached,attached,not_attached}" 2>&1`
TMUX_FOUND=0
for i in $TMUX_SESSIONS
do
IS_ATTACHED=`echo $i | awk '{split($0,array,"ABC"); print array[2]}'`
if [[ $IS_ATTACHED == "not_attached" ]]; then
TMUX_FOUND=1
tmux attach -t `echo $i | awk '{split($0,array,"ABC"); print array[1]}'`
break
fi
done
if [[ $TMUX_FOUND -eq 0 ]];then
tmux new-session
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment