Skip to content

Instantly share code, notes, and snippets.

@bhenderson
Created July 24, 2013 15:45
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 bhenderson/6071785 to your computer and use it in GitHub Desktop.
Save bhenderson/6071785 to your computer and use it in GitHub Desktop.
tmux session management by directory
#!/bin/bash
attach() {
tmux attach-session -t ${1:-$name}
}
list() {
if [[ "$1" == 'short' ]]; then
local args='-F #{session_name}'
shift
fi
tmux list-session $args | grep "$@" "${name:-$}"
}
exact_match() {
list short --quiet -x && attach
}
# no match, create new
# one match, use it
# two matches, show options
close_match() {
# if exact_match is not empty, return 1
test -z "$exact_match" || return
matches=( $(list short) )
case ${#matches[*]} in
0) # no match
return
;;
1) # closest match
attach $matches
;;
*)
# disambiguate
list
;;
esac
}
new() {
# create new windows for each argument
tmux new-session -d -s $name "$1"
shift; let i=1
for arg; do
tmux new-window -t $name:$i "$arg"
let i++
done
tmux select-window -t $name:0
tmux attach-session -t $name
}
name=$1
shift
if [ -z "$name" ]; then
list
exit
fi
# $name is a valid directory, go to it and start tmux
if [ -d "$name" ]; then
cd $name
# force exact match
exact_match='true'
name=`basename $PWD`
fi
exact_match || close_match || new "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment