-
Notifications
You must be signed in to change notification settings - Fork 2
/
.bash_tmux_specific
139 lines (125 loc) · 3.27 KB
/
.bash_tmux_specific
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# vim: set ft=sh:
# Is tmux installed?
tmux_is_installed() {
&>/dev/null which tmux
}
get_additional_windows_csv() {
local session="$1"
env_key="ADDITIONAL_TMUX_WINDOWS_CSV_$(tr '[:lower:]' '[:upper:]' <<< "$session")"
default_env_key="ADDITIONAL_TMUX_WINDOWS_CSV_DEFAULT"
for k in "$env_key" "$default_env_key"
do
v="${!k}"
test -z "$v" && continue
echo "$v"
return 0
done
log_error "$env_key or $default_env_key not defined."
return 1
}
add_additional_tmux_windows() {
local session="$1"
additional_windows_csv="$(get_additional_windows_csv "$session")"
test -z "$additional_windows_csv" && return 0
tmux display-message "Additional windows: $additional_windows_csv"
idx=1
for window in $(echo "$additional_windows_csv" | tr ',' '\n')
do
tmux new-window -t "$session:$idx" -n "$window"
idx=$((idx+1))
done
}
# Are we in an SSH session?
in_ssh_session() {
! test -z "$SSH_CLIENT"
}
# Are we in a TMUX shell already?
in_tmux_session() {
! test -z "$TMUX"
}
tmux_is_disabled() {
! test -z "$DISABLE_TMUX"
}
# Is tmux supported by this OS?
tmux_is_supported() {
os=$(get_os_type)
case "$os" in
Darwin|Ubuntu|Debian)
return 0
;;
*)
return 1
;;
esac
}
tmux_session_is_present() {
test "$(tmux ls 2>/dev/null | wc -l)" -gt "0"
}
join_tmux_session() {
tmux a -t 0
}
reattach_to_user_namespace_not_found() {
! test -f "$(which reattach-to-user-namespace 2>/dev/null)"
}
# Starts a new tmux session with my usual window configuration.
start_tmux() {
start_tmux_with_multiple_windows() {
local session="$1"
tmux new-session -d -s "$session" && \
add_additional_tmux_windows "$session" && \
tmux select-window -t "$session:0" && \
tmux attach-session -t "$session"
}
start_tmux_with_single_window() {
local session="$1"
tmux new-session -d -s "$session" && \
tmux select-pane -t 0 && \
tmux attach-session -t "$session"
}
session_name="${1:-$TMUX_SESSION_NAME}"
cd "$HOME" || return
if reattach_to_user_namespace_not_found
then
log_error "reattach-to-user-namespace not installed or found in \
\$PATH. Please install it with 'brew install reattach-to-user-namespace' and then
'source $HOME/.bash_profile' again."
return 1
fi
if tmux ls | grep -q "$session_name"
then
tmux attach -t "$session_name" 2>/dev/null
else
if in_ssh_session
then start_tmux_with_single_window "$session_name"
else start_tmux_with_multiple_windows "$session_name"
fi
fi
}
# Installs tmux
install_tmux_and_tpm() {
install_tmux() {
case "$(get_os_type)" in
"Darwin")
brew install tmux bash-completion reattach-to-user-namespace
;;
"Ubuntu"|"Debian")
sudo add-apt-repository -y ppa:pi-rho/dev sudo apt-get update
install_application tmux python-software-properties software-properties-common tmux-next
;;
*)
log_info "tmux not supported by this operating system."
return 0
;;
esac
}
install_tpm() {
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm;
}
install_tmux_yank() {
if [ ! -d "$HOME/.tmux.d" ]
then
git clone https://github.com/tmux-plugins/tmux-yank ~/.tmux.d
fi
}
install_tmux && install_tpm && install_tmux_yank
}