Last active
December 28, 2015 13:49
-
-
Save indirect/7510753 to your computer and use it in GitHub Desktop.
tmux session manager. invoke with `tmux-named NAME` to jump to a set of tmux windows with that name. switch to another session the same way. I suggest changing your Terminal application's "new window" command to `/path/to/tmux-named main`. I also create a bash alias named `mux`, because it's short and easy to type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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