blob: a1b8f0694863cac200371b40f4bd39847b3afefc [file] [log] [blame]
Eitan Adler3fc60b52012-04-17 00:24:58 -04001#!/usr/bin/env bash
Travis Swicegoodf6d93252012-02-11 15:39:40 -06002
Jon Schewe9e808642012-02-10 21:01:10 -06003# tmux completion
4# See: http://www.debian-administration.org/articles/317 for how to write more.
5# Usage: Put "source bash_completion_tmux.sh" into your .bashrc
6# Based upon the example at http://paste-it.appspot.com/Pj4mLycDE
7
Von Welche2e63c92012-06-30 16:47:13 -04008 _tmux_cmds=" \
9attach-session \
10bind-key \
11break-pane \
12capture-pane \
13choose-client \
14choose-session \
15choose-window \
16clear-history \
17clock-mode \
18command-prompt \
19confirm-before \
20copy-buffer \
21copy-mode \
22delete-buffer \
23detach-client \
24display-message \
25display-panes \
26down-pane \
27find-window \
28has-session \
29if-shell \
30join-pane \
31kill-pane \
32kill-server \
33kill-session \
34kill-window \
35last-window \
36link-window \
37list-buffers \
38list-clients \
39list-commands \
40list-keys \
41list-panes \
42list-sessions \
43list-windows \
44load-buffer \
45lock-client \
46lock-server \
47lock-session \
48move-window \
49new-session \
50new-window \
51next-layout \
52next-window \
53paste-buffer \
54pipe-pane \
55previous-layout \
56previous-window \
57refresh-client \
58rename-session \
59rename-window \
60resize-pane \
61respawn-window \
62rotate-window \
63run-shell \
64save-buffer \
65select-layout \
66select-pane \
67select-prompt \
68select-window \
69send-keys \
70send-prefix \
71server-info \
72set-buffer \
73set-environment \
74set-option \
75set-window-option \
76show-buffer \
77show-environment \
78show-messages \
79show-options \
80show-window-options \
81source-file \
82split-window \
83start-server \
84suspend-client \
85swap-pane \
86swap-window \
87switch-client \
88unbind-key \
89unlink-window \
90up-pane"
91
Jon Schewe9b866fa2012-02-12 12:30:07 -060092_tmux_expand ()
93{
94 [ "$cur" != "${cur%\\}" ] && cur="$cur"'\';
95 if [[ "$cur" == \~*/* ]]; then
96 eval cur=$cur;
97 else
98 if [[ "$cur" == \~* ]]; then
99 cur=${cur#\~};
100 COMPREPLY=($( compgen -P '~' -u $cur ));
101 return ${#COMPREPLY[@]};
102 fi;
103 fi
104}
105
Jon Schewe98c9bc12012-02-12 12:14:48 -0600106_tmux_filedir ()
107{
108 local IFS='
109';
Jon Schewe9b866fa2012-02-12 12:30:07 -0600110 _tmux_expand || return 0;
Jon Schewe98c9bc12012-02-12 12:14:48 -0600111 if [ "$1" = -d ]; then
112 COMPREPLY=(${COMPREPLY[@]} $( compgen -d -- $cur ));
113 return 0;
114 fi;
115 COMPREPLY=(${COMPREPLY[@]} $( eval compgen -f -- \"$cur\" ))
116}
117
Jon Schewe9e808642012-02-10 21:01:10 -0600118function _tmux_complete_client() {
119 local IFS=$'\n'
120 local cur="${1}"
Von Welche2e63c92012-06-30 16:47:13 -0400121 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-clients 2>/dev/null | cut -f 1 -d ':')" -- "${cur}") )
Jon Schewe9e808642012-02-10 21:01:10 -0600122}
123function _tmux_complete_session() {
124 local IFS=$'\n'
125 local cur="${1}"
Von Welche2e63c92012-06-30 16:47:13 -0400126 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-sessions 2>/dev/null | cut -f 1 -d ':')" -- "${cur}") )
Jon Schewe9e808642012-02-10 21:01:10 -0600127}
128function _tmux_complete_window() {
129 local IFS=$'\n'
130 local cur="${1}"
131 local session_name="$(echo "${cur}" | sed 's/\\//g' | cut -d ':' -f 1)"
132 local sessions
133
Von Welche2e63c92012-06-30 16:47:13 -0400134 sessions="$(tmux -q list-sessions 2>/dev/null | sed -re 's/([^:]+:).*$/\1/')"
Jon Schewe9e808642012-02-10 21:01:10 -0600135 if [[ -n "${session_name}" ]]; then
136 sessions="${sessions}
Von Welche2e63c92012-06-30 16:47:13 -0400137$(tmux -q list-windows -t "${session_name}" 2>/dev/null | sed -re 's/^([^:]+):.*$/'"${session_name}"':\1/')"
Jon Schewe9e808642012-02-10 21:01:10 -0600138 fi
139 cur="$(echo "${cur}" | sed -e 's/:/\\\\:/')"
140 sessions="$(echo "${sessions}" | sed -e 's/:/\\\\:/')"
141 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${sessions}" -- "${cur}") )
142}
143
144_tmux() {
145 local cur prev
146 local i cmd cmd_index option option_index
147 local opts=""
148 COMPREPLY=()
149 cur="${COMP_WORDS[COMP_CWORD]}"
150 prev="${COMP_WORDS[COMP_CWORD-1]}"
151
152 if [ ${prev} == -f ]; then
Jon Schewe98c9bc12012-02-12 12:14:48 -0600153 _tmux_filedir
Jon Schewe9e808642012-02-10 21:01:10 -0600154 else
155 # Search for the command
156 local skip_next=0
157 for ((i=1; $i<=$COMP_CWORD; i++)); do
158 if [[ ${skip_next} -eq 1 ]]; then
159 #echo "Skipping"
160 skip_next=0;
161 elif [[ ${COMP_WORDS[i]} != -* ]]; then
162 cmd="${COMP_WORDS[i]}"
163 cmd_index=${i}
164 break
165 elif [[ ${COMP_WORDS[i]} == -f ]]; then
166 skip_next=1
167 fi
168 done
169
170 # Search for the last option command
171 skip_next=0
172 for ((i=1; $i<=$COMP_CWORD; i++)); do
173 if [[ ${skip_next} -eq 1 ]]; then
174 #echo "Skipping"
175 skip_next=0;
176 elif [[ ${COMP_WORDS[i]} == -* ]]; then
177 option="${COMP_WORDS[i]}"
178 option_index=${i}
179 if [[ ${COMP_WORDS[i]} == -- ]]; then
180 break;
181 fi
182 elif [[ ${COMP_WORDS[i]} == -f ]]; then
183 skip_next=1
184 fi
185 done
186
187 if [[ $COMP_CWORD -le $cmd_index ]]; then
188 # The user has not specified a command yet
Von Welche2e63c92012-06-30 16:47:13 -0400189 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${_tmux_cmds}" -- "${cur}") )
Jon Schewe9e808642012-02-10 21:01:10 -0600190 else
191 case ${cmd} in
192 attach-session|attach)
193 case "$prev" in
194 -t) _tmux_complete_session "${cur}" ;;
195 *) options="-t -d" ;;
196 esac ;;
197 detach-client|detach)
198 case "$prev" in
199 -t) _tmux_complete_client "${cur}" ;;
200 *) options="-t" ;;
201 esac ;;
202 lock-client|lockc)
203 case "$prev" in
204 -t) _tmux_complete_client "${cur}" ;;
205 *) options="-t" ;;
206 esac ;;
207 lock-session|locks)
208 case "$prev" in
209 -t) _tmux_complete_session "${cur}" ;;
210 *) options="-t -d" ;;
211 esac ;;
Travis Swicegoodf6d93252012-02-11 15:39:40 -0600212 new-session|new)
Jon Schewe9e808642012-02-10 21:01:10 -0600213 case "$prev" in
214 -t) _tmux_complete_session "${cur}" ;;
Jon Schewe92c97592012-02-11 14:48:09 -0600215 -[n|d|s]) options="-d -n -s -t --" ;;
Jon Schewe9e808642012-02-10 21:01:10 -0600216 *)
217 if [[ ${COMP_WORDS[option_index]} == -- ]]; then
218 _command_offset ${option_index}
219 else
220 options="-d -n -s -t --"
221 fi
222 ;;
223 esac
224 ;;
225 refresh-client|refresh)
226 case "$prev" in
227 -t) _tmux_complete_client "${cur}" ;;
228 *) options="-t" ;;
229 esac ;;
230 rename-session|rename)
231 case "$prev" in
232 -t) _tmux_complete_session "${cur}" ;;
233 *) options="-t" ;;
234 esac ;;
Jon Schewe98c9bc12012-02-12 12:14:48 -0600235 source-file|source) _tmux_filedir ;;
Jon Schewe9e808642012-02-10 21:01:10 -0600236 has-session|has|kill-session)
237 case "$prev" in
238 -t) _tmux_complete_session "${cur}" ;;
239 *) options="-t" ;;
240 esac ;;
241 suspend-client|suspendc)
242 case "$prev" in
243 -t) _tmux_complete_client "${cur}" ;;
244 *) options="-t" ;;
245 esac ;;
246 switch-client|switchc)
247 case "$prev" in
248 -c) _tmux_complete_client "${cur}" ;;
249 -t) _tmux_complete_session "${cur}" ;;
250 *) options="-l -n -p -c -t" ;;
251 esac ;;
252
253 send-keys|send)
254 case "$option" in
255 -t) _tmux_complete_window "${cur}" ;;
256 *) options="-t" ;;
257 esac ;;
258 esac # case ${cmd}
259 fi # command specified
260 fi # not -f
261
262 if [[ -n "${options}" ]]; then
263 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${options}" -- "${cur}") )
264 fi
265
266 return 0
267
268}
269complete -F _tmux tmux
270
271# END tmux completion
272