-
-
Save bhenderson/6071785 to your computer and use it in GitHub Desktop.
tmux session management by directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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