Skip to content

Instantly share code, notes, and snippets.

@nnutter
Forked from ttscoff/tm.bash
Last active August 29, 2015 14:01
Show Gist options
  • Save nnutter/ef39bd68d17cba60db16 to your computer and use it in GitHub Desktop.
Save nnutter/ef39bd68d17cba60db16 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 2> /dev/null | awk -F ":" '{ print $1 }'`;do
if [[ $session =~ ^$1 ]]; then
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
window=$win
break
fi
done
tmux attach -t $attach:$window > /dev/null
else
tmux attach -t $attach > /dev/null
fi
else
if [ $# -gt 1 ]; then
attach=$1
shift
tmux new -s $attach -n $1 > /dev/null
else
tmux new -s $1 > /dev/null
fi
fi
else
tmux new > /dev/null
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment