Skip to content

Instantly share code, notes, and snippets.

@arkku
Last active October 5, 2017 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkku/e5aa95e8dd08718fc956f6d9fc6cb70e to your computer and use it in GitHub Desktop.
Save arkku/e5aa95e8dd08718fc956f6d9fc6cb70e to your computer and use it in GitHub Desktop.
Arkku's tmux wrapper script
#!/bin/sh
# A wrapper for running tmux. Takes on (optional) main session name as
# argument, creates it if it doesn't exist. Then (in either case),
# a new slave session is created and attached to the main session.
# The slave session is configured to be destroyed automatically when
# detached, but the main session stays alive. This arrangement allows
# each slave session to have independent control over active windows/panes.
#
# (Make sure ~/.tmux.conf does not interfere by having something like
# `new-session` or `attach-session` in it.)
#
# By Kimmo Kulovesi <http://arkku.com/> 2017-02-03.
# Distribute freely, use at your own risk only.
if [ -n "$TMUX" ]; then
echo 'Error: Already in tmux!' >&2
exit 1
fi
if [ -z "$1" ]; then
main_session=0
elif [ "$1" = "ls" ]; then
exec tmux ls
else
main_session="$1"
fi
if ! tmux ls 2>/dev/null | grep -q "^$main_session:"; then
echo "New session: $main_session" >&2
tmux new-session -d -s "$main_session" || exit 1
fi
if [ -z "$2" ]; then
slave_session="${main_session}_$$"
else
slave_session="${main_session}_$2"
if tmux ls 2>/dev/null | grep -q "^$slave_session:"; then
slave_session="${slave_session}_$$"
fi
fi
echo "Attaching to $main_session as $slave_session" >&2
exec tmux new-session -d -t "$main_session" -s "$slave_session" \; \
set-option destroy-unattached \; \
attach-session -t "$slave_session"
@arkku
Copy link
Author

arkku commented Oct 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment