Skip to content

Instantly share code, notes, and snippets.

@macdiva
Forked from indirect/tmux-named
Created November 19, 2013 15:42
Show Gist options
  • Save macdiva/7547312 to your computer and use it in GitHub Desktop.
Save macdiva/7547312 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Set up paths and whatnot
test -e ~/.bashrc && source ~/.bashrc
# 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= 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
# Create the new session
TMUX= 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