Skip to content

Instantly share code, notes, and snippets.

@dentearl
Last active January 2, 2016 18:19
Show Gist options
  • Save dentearl/8342740 to your computer and use it in GitHub Desktop.
Save dentearl/8342740 to your computer and use it in GitHub Desktop.
Script to provide a simple interface to tmux, a la run_screen
#!/bin/bash
# adapted from run_screen, from the Bash Cookbook, 1st ed. May 2007
# Albing, Vossen and Newham
# adapted by dent earl, dent earl at gmail
##############################
# Sanity check
if [ "$TERM" == "screen" ]; then
printf "%b" "According to '\$TERM' we're using screen right now. \nAborting.\n"
exit 1
elif [ "$_USING_TMUX" == "YES" ]; then
printf "%b" "According to '\$_USING_TMUX' we're using tmux right now. \nAborting.\n"
exit 1
elif [ "$USING_SCREEN" == "YES" ]; then
printf "%b" "According to '\$USING_SCREEN' we're using screen right now. \nAborting.\n"
exit 1
elif [[ ! -z "$TMUX" ]]; then
printf "%b" "According to '\$TMUX' we're using tmux right now. \nAborting.\n"
exit 1
fi
# The "$_USING_TMUX" variable is for the rare cases when tmux does
# NOT set $TMUX=screen. This can happen when 'tmux' is not in
# TERMCAP or friends. If we don't have some way to tell when we're inside
# tmux, this wrapper goes into an ugly and confusing endless loop.
# Seed list with Exit and New options and see what screens are already
# running; The select list is white space delimited, and we only want
# actual screen sessions, so use awk to filter for those, then remove
# any tabs from 'screen -ls' output.
available_tmux="Exit New $(tmux ls | awk '{print $1}' | perl -ple 's/://')"
# Present a list of choices
P3S='Choose a tmux for this session: '
select selection in $available_tmux; do
if [ "$selection" == "Exit" ]; then
break
elif [ "$selection" == "New" ]; then
export _USING_TMUX=YES
exec tmux new -s $USER.$(date '+%Y-%m-%d_%H%M%S%z')
break
elif [ "$selection" ]; then
exec tmux a -t $selection
break
else
printf "%b" "Invalid selection.\n"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment