-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmux_goto
executable file
·54 lines (49 loc) · 1.13 KB
/
tmux_goto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
if [ "$1" == "-h" -o "$1" == "--help" ]
then
cat <<END >&2
Switch to the tmux session matching REGEX.
If no REGEX is provided, all sessions are listed.
If there are more than one options, present a menu.
USAGE: $(basename $0) [REGEX]
END
exit 1
fi
IFS='
'
SEARCH="$1"
if [ -z "$SEARCH" ]
then
RES=$(tmux list-windows -a) || {
echo "tmux list-windows failed or there were no windows matching '$SEARCH'" >&2
exit 2
}
else
RES=$(tmux list-windows -a | grep -i "$SEARCH") || {
echo "tmux list-windows failed or there were no windows matching '$SEARCH'" >&2
exit 2
}
fi
NRES=$(echo "$RES" | wc -l)
function menu() {
n=0
for r in $RES
do
let n=$[ $n + 1 ]
echo "$n. $r"
done
read -p "Select window number: " SEL
}
if [ $NRES -eq 1 ]
then
TARGET=$(echo "$RES" | cut -d: -f1,2)
else
menu
# TODO: sanity-check sel, re-run menu if dumb
[ -z "$SEL" ] && {
echo "No selection made. Exiting."
exit
}
TARGET=$(echo "$RES" | head -n+$SEL | tail -n1 | cut -d: -f1,2)
fi
[ -n "$TARGET" ] && tmux switch-client -t $TARGET