This repository has been archived by the owner on Dec 26, 2020. It is now read-only.
forked from Bash-it/bash-it
/
tmux.completion.bash
272 lines (256 loc) · 6.87 KB
/
tmux.completion.bash
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env bash
# tmux completion
# See: http://www.debian-administration.org/articles/317 for how to write more.
# Usage: Put "source bash_completion_tmux.sh" into your .bashrc
# Based upon the example at http://paste-it.appspot.com/Pj4mLycDE
_tmux_cmds=" \
attach-session \
bind-key \
break-pane \
capture-pane \
choose-client \
choose-session \
choose-window \
clear-history \
clock-mode \
command-prompt \
confirm-before \
copy-buffer \
copy-mode \
delete-buffer \
detach-client \
display-message \
display-panes \
down-pane \
find-window \
has-session \
if-shell \
join-pane \
kill-pane \
kill-server \
kill-session \
kill-window \
last-window \
link-window \
list-buffers \
list-clients \
list-commands \
list-keys \
list-panes \
list-sessions \
list-windows \
load-buffer \
lock-client \
lock-server \
lock-session \
move-window \
new-session \
new-window \
next-layout \
next-window \
paste-buffer \
pipe-pane \
previous-layout \
previous-window \
refresh-client \
rename-session \
rename-window \
resize-pane \
respawn-window \
rotate-window \
run-shell \
save-buffer \
select-layout \
select-pane \
select-prompt \
select-window \
send-keys \
send-prefix \
server-info \
set-buffer \
set-environment \
set-option \
set-window-option \
show-buffer \
show-environment \
show-messages \
show-options \
show-window-options \
source-file \
split-window \
start-server \
suspend-client \
swap-pane \
swap-window \
switch-client \
unbind-key \
unlink-window \
up-pane"
_tmux_expand ()
{
[ "$cur" != "${cur%\\}" ] && cur="$cur"'\';
if [[ "$cur" == \~*/* ]]; then
eval cur=$cur;
else
if [[ "$cur" == \~* ]]; then
cur=${cur#\~};
COMPREPLY=($( compgen -P '~' -u $cur ));
return ${#COMPREPLY[@]};
fi;
fi
}
_tmux_filedir ()
{
local IFS='
';
_tmux_expand || return 0;
if [ "$1" = -d ]; then
COMPREPLY=(${COMPREPLY[@]} $( compgen -d -- $cur ));
return 0;
fi;
COMPREPLY=(${COMPREPLY[@]} $( eval compgen -f -- \"$cur\" ))
}
function _tmux_complete_client() {
local IFS=$'\n'
local cur="${1}"
COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-clients 2>/dev/null | cut -f 1 -d ':')" -- "${cur}") )
}
function _tmux_complete_session() {
local IFS=$'\n'
local cur="${1}"
COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-sessions 2>/dev/null | cut -f 1 -d ':')" -- "${cur}") )
}
function _tmux_complete_window() {
local IFS=$'\n'
local cur="${1}"
local session_name="$(echo "${cur}" | sed 's/\\//g' | cut -d ':' -f 1)"
local sessions
sessions="$(tmux -q list-sessions 2>/dev/null | sed -re 's/([^:]+:).*$/\1/')"
if [[ -n "${session_name}" ]]; then
sessions="${sessions}
$(tmux -q list-windows -t "${session_name}" 2>/dev/null | sed -re 's/^([^:]+):.*$/'"${session_name}"':\1/')"
fi
cur="$(echo "${cur}" | sed -e 's/:/\\\\:/')"
sessions="$(echo "${sessions}" | sed -e 's/:/\\\\:/')"
COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${sessions}" -- "${cur}") )
}
_tmux() {
local cur prev
local i cmd cmd_index option option_index
local opts=""
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ ${prev} == -f ]; then
_tmux_filedir
else
# Search for the command
local skip_next=0
for ((i=1; $i<=$COMP_CWORD; i++)); do
if [[ ${skip_next} -eq 1 ]]; then
#echo "Skipping"
skip_next=0;
elif [[ ${COMP_WORDS[i]} != -* ]]; then
cmd="${COMP_WORDS[i]}"
cmd_index=${i}
break
elif [[ ${COMP_WORDS[i]} == -f ]]; then
skip_next=1
fi
done
# Search for the last option command
skip_next=0
for ((i=1; $i<=$COMP_CWORD; i++)); do
if [[ ${skip_next} -eq 1 ]]; then
#echo "Skipping"
skip_next=0;
elif [[ ${COMP_WORDS[i]} == -* ]]; then
option="${COMP_WORDS[i]}"
option_index=${i}
if [[ ${COMP_WORDS[i]} == -- ]]; then
break;
fi
elif [[ ${COMP_WORDS[i]} == -f ]]; then
skip_next=1
fi
done
if [[ $COMP_CWORD -le $cmd_index ]]; then
# The user has not specified a command yet
COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${_tmux_cmds}" -- "${cur}") )
else
case ${cmd} in
attach-session|attach)
case "$prev" in
-t) _tmux_complete_session "${cur}" ;;
*) options="-t -d" ;;
esac ;;
detach-client|detach)
case "$prev" in
-t) _tmux_complete_client "${cur}" ;;
*) options="-t" ;;
esac ;;
lock-client|lockc)
case "$prev" in
-t) _tmux_complete_client "${cur}" ;;
*) options="-t" ;;
esac ;;
lock-session|locks)
case "$prev" in
-t) _tmux_complete_session "${cur}" ;;
*) options="-t -d" ;;
esac ;;
new-session|new)
case "$prev" in
-t) _tmux_complete_session "${cur}" ;;
-[n|d|s]) options="-d -n -s -t --" ;;
*)
if [[ ${COMP_WORDS[option_index]} == -- ]]; then
_command_offset ${option_index}
else
options="-d -n -s -t --"
fi
;;
esac
;;
refresh-client|refresh)
case "$prev" in
-t) _tmux_complete_client "${cur}" ;;
*) options="-t" ;;
esac ;;
rename-session|rename)
case "$prev" in
-t) _tmux_complete_session "${cur}" ;;
*) options="-t" ;;
esac ;;
source-file|source) _tmux_filedir ;;
has-session|has|kill-session)
case "$prev" in
-t) _tmux_complete_session "${cur}" ;;
*) options="-t" ;;
esac ;;
suspend-client|suspendc)
case "$prev" in
-t) _tmux_complete_client "${cur}" ;;
*) options="-t" ;;
esac ;;
switch-client|switchc)
case "$prev" in
-c) _tmux_complete_client "${cur}" ;;
-t) _tmux_complete_session "${cur}" ;;
*) options="-l -n -p -c -t" ;;
esac ;;
send-keys|send)
case "$option" in
-t) _tmux_complete_window "${cur}" ;;
*) options="-t" ;;
esac ;;
esac # case ${cmd}
fi # command specified
fi # not -f
if [[ -n "${options}" ]]; then
COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${options}" -- "${cur}") )
fi
return 0
}
complete -F _tmux tmux
# END tmux completion