Skip to content

Instantly share code, notes, and snippets.

@jrconlin
Created May 31, 2024 15:54
Show Gist options
  • Save jrconlin/6e4e3032380aafa9c3bafc97ac9bfa24 to your computer and use it in GitHub Desktop.
Save jrconlin/6e4e3032380aafa9c3bafc97ac9bfa24 to your computer and use it in GitHub Desktop.
tmx - A session smart TMUX launch script
#!/bin/bash
#
# Modified TMUX start script from:
# http://forums.gentoo.org/viewtopic-t-836006-start-0.html
#
# Store it to `~/bin/tmx` and issue `chmod +x`.
#
# Works because bash automatically trims by assigning to variables and by
# passing arguments
trim() { echo $1; }
SNAME=$1
if [[ -z "$SNAME" ]]; then
echo "Using session 'Main'"
SNAME="Main"
fi
# Only because I often issue `ls` to this script by accident
if [[ "$SNAME" == "ls" ]]; then
tmux ls
exit
fi
base_session="$SNAME"
# This actually works without the trim() on all systems except OSX
tmux_nb=$(trim `tmux ls | grep "^$base_session" | wc -l`)
if [[ "$tmux_nb" == "0" ]]; then
echo "Launching tmux base session $base_session ..."
tmux new-session -s $base_session
else
# Make sure we are not already in a tmux session
if [[ -z "$TMUX" ]]; then
echo "Launching copy of base session $base_session ..."
# Session is is date and time to prevent conflict
session_id=`date +%Y%m%d%H%M%S`
# Create a new session (without attaching it) and link to base session
# to share windows
tmux new-session -d -t $base_session -s $session_id
if [[ "$2" == "1" ]]; then
# Create a new window in that session
tmux new-window
fi
# Attach to the new session & kill it once orphaned
tmux attach-session -t $session_id \; set-option destroy-unattached
fi
fi
stty sane
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment