blob: 87008a344bfe02801373d21a552ce82dd9b491a5 [file] [log] [blame]
Jon Schewe9e808642012-02-10 21:01:10 -06001# tmux completion
2# See: http://www.debian-administration.org/articles/317 for how to write more.
3# Usage: Put "source bash_completion_tmux.sh" into your .bashrc
4# Based upon the example at http://paste-it.appspot.com/Pj4mLycDE
5
Jon Schewe9b866fa2012-02-12 12:30:07 -06006_tmux_expand ()
7{
8 [ "$cur" != "${cur%\\}" ] && cur="$cur"'\';
9 if [[ "$cur" == \~*/* ]]; then
10 eval cur=$cur;
11 else
12 if [[ "$cur" == \~* ]]; then
13 cur=${cur#\~};
14 COMPREPLY=($( compgen -P '~' -u $cur ));
15 return ${#COMPREPLY[@]};
16 fi;
17 fi
18}
19
Jon Schewe98c9bc12012-02-12 12:14:48 -060020_tmux_filedir ()
21{
22 local IFS='
23';
Jon Schewe9b866fa2012-02-12 12:30:07 -060024 _tmux_expand || return 0;
Jon Schewe98c9bc12012-02-12 12:14:48 -060025 if [ "$1" = -d ]; then
26 COMPREPLY=(${COMPREPLY[@]} $( compgen -d -- $cur ));
27 return 0;
28 fi;
29 COMPREPLY=(${COMPREPLY[@]} $( eval compgen -f -- \"$cur\" ))
30}
31
Jon Schewe9e808642012-02-10 21:01:10 -060032function _tmux_complete_client() {
33 local IFS=$'\n'
34 local cur="${1}"
35 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-clients | cut -f 1 -d ':')" -- "${cur}") )
36}
37function _tmux_complete_session() {
38 local IFS=$'\n'
39 local cur="${1}"
40 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-sessions | cut -f 1 -d ':')" -- "${cur}") )
41}
42function _tmux_complete_window() {
43 local IFS=$'\n'
44 local cur="${1}"
45 local session_name="$(echo "${cur}" | sed 's/\\//g' | cut -d ':' -f 1)"
46 local sessions
47
48 sessions="$(tmux -q list-sessions | sed -re 's/([^:]+:).*$/\1/')"
49 if [[ -n "${session_name}" ]]; then
50 sessions="${sessions}
51$(tmux -q list-windows -t "${session_name}" | sed -re 's/^([^:]+):.*$/'"${session_name}"':\1/')"
52 fi
53 cur="$(echo "${cur}" | sed -e 's/:/\\\\:/')"
54 sessions="$(echo "${sessions}" | sed -e 's/:/\\\\:/')"
55 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${sessions}" -- "${cur}") )
56}
57
58_tmux() {
59 local cur prev
60 local i cmd cmd_index option option_index
61 local opts=""
62 COMPREPLY=()
63 cur="${COMP_WORDS[COMP_CWORD]}"
64 prev="${COMP_WORDS[COMP_CWORD-1]}"
65
66 if [ ${prev} == -f ]; then
Jon Schewe98c9bc12012-02-12 12:14:48 -060067 _tmux_filedir
Jon Schewe9e808642012-02-10 21:01:10 -060068 else
69 # Search for the command
70 local skip_next=0
71 for ((i=1; $i<=$COMP_CWORD; i++)); do
72 if [[ ${skip_next} -eq 1 ]]; then
73 #echo "Skipping"
74 skip_next=0;
75 elif [[ ${COMP_WORDS[i]} != -* ]]; then
76 cmd="${COMP_WORDS[i]}"
77 cmd_index=${i}
78 break
79 elif [[ ${COMP_WORDS[i]} == -f ]]; then
80 skip_next=1
81 fi
82 done
83
84 # Search for the last option command
85 skip_next=0
86 for ((i=1; $i<=$COMP_CWORD; i++)); do
87 if [[ ${skip_next} -eq 1 ]]; then
88 #echo "Skipping"
89 skip_next=0;
90 elif [[ ${COMP_WORDS[i]} == -* ]]; then
91 option="${COMP_WORDS[i]}"
92 option_index=${i}
93 if [[ ${COMP_WORDS[i]} == -- ]]; then
94 break;
95 fi
96 elif [[ ${COMP_WORDS[i]} == -f ]]; then
97 skip_next=1
98 fi
99 done
100
101 if [[ $COMP_CWORD -le $cmd_index ]]; then
102 # The user has not specified a command yet
103 local all_commands="$(tmux -q list-commands | cut -f 1 -d ' ')"
104 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${all_commands}" -- "${cur}") )
105 else
106 case ${cmd} in
107 attach-session|attach)
108 case "$prev" in
109 -t) _tmux_complete_session "${cur}" ;;
110 *) options="-t -d" ;;
111 esac ;;
112 detach-client|detach)
113 case "$prev" in
114 -t) _tmux_complete_client "${cur}" ;;
115 *) options="-t" ;;
116 esac ;;
117 lock-client|lockc)
118 case "$prev" in
119 -t) _tmux_complete_client "${cur}" ;;
120 *) options="-t" ;;
121 esac ;;
122 lock-session|locks)
123 case "$prev" in
124 -t) _tmux_complete_session "${cur}" ;;
125 *) options="-t -d" ;;
126 esac ;;
127 new-sesison|new)
128 case "$prev" in
129 -t) _tmux_complete_session "${cur}" ;;
Jon Schewe92c97592012-02-11 14:48:09 -0600130 -[n|d|s]) options="-d -n -s -t --" ;;
Jon Schewe9e808642012-02-10 21:01:10 -0600131 *)
132 if [[ ${COMP_WORDS[option_index]} == -- ]]; then
133 _command_offset ${option_index}
134 else
135 options="-d -n -s -t --"
136 fi
137 ;;
138 esac
139 ;;
140 refresh-client|refresh)
141 case "$prev" in
142 -t) _tmux_complete_client "${cur}" ;;
143 *) options="-t" ;;
144 esac ;;
145 rename-session|rename)
146 case "$prev" in
147 -t) _tmux_complete_session "${cur}" ;;
148 *) options="-t" ;;
149 esac ;;
Jon Schewe98c9bc12012-02-12 12:14:48 -0600150 source-file|source) _tmux_filedir ;;
Jon Schewe9e808642012-02-10 21:01:10 -0600151 has-session|has|kill-session)
152 case "$prev" in
153 -t) _tmux_complete_session "${cur}" ;;
154 *) options="-t" ;;
155 esac ;;
156 suspend-client|suspendc)
157 case "$prev" in
158 -t) _tmux_complete_client "${cur}" ;;
159 *) options="-t" ;;
160 esac ;;
161 switch-client|switchc)
162 case "$prev" in
163 -c) _tmux_complete_client "${cur}" ;;
164 -t) _tmux_complete_session "${cur}" ;;
165 *) options="-l -n -p -c -t" ;;
166 esac ;;
167
168 send-keys|send)
169 case "$option" in
170 -t) _tmux_complete_window "${cur}" ;;
171 *) options="-t" ;;
172 esac ;;
173 esac # case ${cmd}
174 fi # command specified
175 fi # not -f
176
177 if [[ -n "${options}" ]]; then
178 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${options}" -- "${cur}") )
179 fi
180
181 return 0
182
183}
184complete -F _tmux tmux
185
186# END tmux completion
187