Skip to content

Instantly share code, notes, and snippets.

@indirect
Created April 25, 2013 17:43
Show Gist options
  • Save indirect/5461601 to your computer and use it in GitHub Desktop.
Save indirect/5461601 to your computer and use it in GitHub Desktop.
Open multiple tmux sessions connected to the same set of windows
#!/bin/bash
# We need tmux. Obvs.
if [[ -z `which tmux` ]]; then echo "You need tmux first!"; exit 1; fi
# Named variables are much more flexible
name="$1"
# Make sure we got a session name to create or join
[[ -n "$name" ]] || { echo "Usage: tmux-named [SESSION_NAME]"; exit; }
# Make sure we can run homebrewed tmux
if [[ $PATH != */usr/local/bin* ]]; then export PATH=$PATH:/usr/local/bin; fi
# Make sure there is a 'name' session
tmux has -t "$name" 2> /dev/null || tmux new -d -s "$name"
# Calculate the number of the next session
session_number=$(tmux ls -F "#S" | grep "^$name" | wc -l | sed "s/^[ ]*//")
session_name="${name} $session_number"
echo $session_name
# If this session already exists, a lower number must be missing
while tmux has -t "$session_name" 2> /dev/null; do
let session_number--
session_name="${name} $session_number"
done
echo $session_name
# Create the new session
tmux new-session -d -t "$name" -s "$session_name"
# Tell this session to die when the window containing it is closed
die="set-option -q -t $session_name destroy-unattached on"
if [ -z $TMUX ]; then
# Join the new and configured session
tmux $die \; attach-session -t "$session_name"
else
# Switch this tmux client to the new session
tmux $die \; switch-client -t "$session_name"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment