Skip to content

Instantly share code, notes, and snippets.

@nefftd
Created March 4, 2015 16:23
Show Gist options
  • Save nefftd/1150ae0158ecf4f866df to your computer and use it in GitHub Desktop.
Save nefftd/1150ae0158ecf4f866df to your computer and use it in GitHub Desktop.
#!/bin/bash
# I only care about tmux session management, and don't care about
# nested sessions. Here's a little wrapper to singicantly ease
# session management at the expense of tmux's more powerful
# features.
usage() {
echo "usage: tm ACTION [name]" 1>&2;
echo "Available actions are:" 1>&2;
echo " a Attach session by \`name\`" 1>&2;
echo " d Detach the current session" 1>&2;
echo " l List sessions" 1>&2;
echo " n Create new session by \`name\`" 1>&2;
echo " x Kill session by \`name\` (default: current session)" 1>&2;
exit 1;
}
if [[ $# == 0 ]]; then
usage
fi
ERRCODE=0
case $1 in
a)
if [[ $# > 1 ]]; then
tmux attach -t $2
else
echo "usage: tm a name" 1>&2;
ERRCODE=1
fi
;;
d)
OUT="$(tmux detach 2>&1)"
if [[ "$OUT" =~ ^(failed to connect to server) ]]; then
echo "tm: tmux server not active" 1>&2;
ERRCODE=1
elif [[ $? != 0 ]]; then
[[ -z "$OUT" ]] && echo $OUT 1>&2;
ERRCODE=1
fi
;;
l)
OUT="$(tmux list-sessions 2>&1)"
if [[ "$OUT" =~ ^(failed to connect to server) ]]; then
echo "tm: tmux server not active" 1>&2;
ERRCODE=1
elif [[ $? != 0 ]]; then
[[ -z "$OUT" ]] && echo $OUT 1>&2;
ERRCODE=1
fi
;;
n)
if [[ $# > 1 ]] ; then
tmux new-session -s $2
else
echo "usage: tm n name" 1>&2;
ERRCODE=1
fi
;;
x)
OUT="$(tmux list-sessions 2>&1)"
if [[ $OUT =~ ^(failed to connect to server) ]]; then
echo "tm: tmux server not active" 1>&2;
ERRCODE=1
else
read -r -p "Are you sure? [y/N] " response
if [[ $response =~ ^\s*([yY]|[yY][eE][sS])\s*$ ]]; then
if [[ $# > 1 ]]; then
tmux kill-session -t $2
else
tmux kill-session
fi
fi
fi
;;
*)
usage
;;
esac
exit $ERRCODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment