Skip to content

Instantly share code, notes, and snippets.

@dragon788
Forked from ttscoff/tm.bash
Last active August 29, 2015 14:14
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 dragon788/b91cb38c8f142c1a951c to your computer and use it in GitHub Desktop.
Save dragon788/b91cb38c8f142c1a951c to your computer and use it in GitHub Desktop.
# Brett Terpstra 2014
# <http://brettterpstra.com>
#
# tmux wrapper
# tm session-name [window-name]
# Names can be partial from the beginning and first match will connect.
# If no match is found a new session will be created.
# If there's a second argument, it will be used to attach directly to a
# window in the session, or to name the first window in a new session.
tm() {
local attach window
if [ -n $1 ]; then
attach=""
tmux has-session -t $1 > /dev/null
if [ $? -eq 0 ]; then
attach=$1
shift
else
for session in `tmux ls|awk -F ":" '{ print $1 }'`;do
if [[ $session =~ ^$1 ]]; then
echo "Matched session: $session"
attach=$session
shift
break
fi
done
fi
if [[ $attach != "" ]]; then
if [ $# -eq 1 ]; then
for win in `tmux list-windows -t $attach|sed -E 's/^[0-9]+: //'|sed -E 's/[*-].*//'`;do
if [[ $win =~ ^$1 ]]; then
echo "Matched window: $window"
window=$win
break
fi
done
tmux attach -t $attach:$window
else
tmux attach -t $attach
fi
else
if [ $# -gt 1 ]; then
attach=$1
shift
tmux new -s $attach -n $1
else
echo "Attempting to create $1"
tmux new -s $1
fi
fi
else
tmux new
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment