forked from brandur/tmux-extra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmx
executable file
·101 lines (88 loc) · 3.33 KB
/
tmx
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
#
# Tmux launcher
#
# See:
# http://github.com/brandur/tmux-extra
#
# Modified version of a script orginally found at:
# http://forums.gentoo.org/viewtopic-t-836006-start-0.html
#
#If we're running inside iTerm, integrate with its tmux support
if [ x"$TERM_PROGRAM" = x"iTerm.app" ]; then
TMUX_CMD="tmux -CC"
else
TMUX_CMD="tmux"
fi
# Works because bash automatically trims by assigning to variables and by
# passing arguments
trim() { echo $1; }
#TMUX_SUPPORTS_MINUS_F=$(tmux list-sessions -F '#{session_name}' > /dev/null 2>&1 && echo 0 || echo 1)
#...but that hangs on Linux due to epoll problems -- see
# https://sourceforge.net/tracker/?func=detail&atid=973262&aid=3489496&group_id=200378
TMUX_SUPPORTS_MINUS_F=1 #disable -F support
tmux-list-sessions() {
if [[ "$TMUX_SUPPORTS_MINUS_F" == 0 ]] ; then
tmux list-sessions -F '#{session_name}'
else
tmux list-sessions | cut -d: -f 1
fi
}
tmux-list-old-sessions() {
if [[ "$TMUX_SUPPORTS_MINUS_F" == 0 ]] ; then
tmux list-sessions -F '#{?session_attached,,#{?session_grouped,,#{session_name}}}'
else
tmux list-sessions 2>/dev/null | grep -v '(group [[:digit:]]\+)' | grep -v '(attached)' | cut -d: -f 1
fi
}
if [[ -z "$1" ]]; then
echo "Specify session name as the first argument"
exit
fi
# Only because I often issue `ls` to this script by accident
if [[ "$1" == "ls" ]]; then
tmux ls
exit
fi
base_session="$1"
# This actually works without the trim() on all systems except OSX
tmux_nb=$(trim `tmux-list-sessions | grep -c "^$base_session$"`)
if [[ "$tmux_nb" == "0" ]]; then
echo "Launching tmux base session $base_session ..."
# we separate 'new-session' and 'attach' to allow .tmux.conf
# commands to create the new session first with the right name
# (which is, after all, a simple convention so easily shared),
# which will (perversely) then cause this new-session command to
# exit with an error. Despite this error, a new session with
# exactly that name has been created. Then we can happily attach
# to it. If .tmux.conf has no new-session command in it, our
# explicit 'new-session' creates it, and we attach happily, again,
# to it.
$TMUX_CMD new-session -d -s $base_session
$TMUX_CMD attach -t $base_session
else
# Make sure we are not already in a tmux session
if [[ -z "$TMUX" ]]; then
if [[ -z "$TMUX_PRESERVE_UNATTACHED_SESSIONS" ]] ; then
# Kill defunct sessions first
for old_session_id in `tmux-list-old-sessions` ; do
tmux kill-session -t $old_session_id
done
fi
echo "Launching copy of base session $base_session ..."
# Find a session prefix number that hasn't already been used
num_prefix=1
while [[ $(trim `tmux-list-sessions | grep "^$num_prefix,$base_session" | wc -l`) -gt 0 ]];
do
num_prefix=`expr $num_prefix + 1`
done
session_id="$num_prefix,$base_session"
# Create a new session (without attaching it) and link to base session
# to share windows
tmux new-session -d -t $base_session -s $session_id
# Attach to the new session
$TMUX_CMD attach-session -t $session_id
# When we detach from it, kill the session
$TMUX_CMD kill-session -t $session_id
fi
fi