blob: 80857ad006950448a93d27f20ccdadb8fbd51cb7 [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
6function _tmux_complete_client() {
7 local IFS=$'\n'
8 local cur="${1}"
9 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-clients | cut -f 1 -d ':')" -- "${cur}") )
10}
11function _tmux_complete_session() {
12 local IFS=$'\n'
13 local cur="${1}"
14 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-sessions | cut -f 1 -d ':')" -- "${cur}") )
15}
16function _tmux_complete_window() {
17 local IFS=$'\n'
18 local cur="${1}"
19 local session_name="$(echo "${cur}" | sed 's/\\//g' | cut -d ':' -f 1)"
20 local sessions
21
22 sessions="$(tmux -q list-sessions | sed -re 's/([^:]+:).*$/\1/')"
23 if [[ -n "${session_name}" ]]; then
24 sessions="${sessions}
25$(tmux -q list-windows -t "${session_name}" | sed -re 's/^([^:]+):.*$/'"${session_name}"':\1/')"
26 fi
27 cur="$(echo "${cur}" | sed -e 's/:/\\\\:/')"
28 sessions="$(echo "${sessions}" | sed -e 's/:/\\\\:/')"
29 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${sessions}" -- "${cur}") )
30}
31
32_tmux() {
33 local cur prev
34 local i cmd cmd_index option option_index
35 local opts=""
36 COMPREPLY=()
37 cur="${COMP_WORDS[COMP_CWORD]}"
38 prev="${COMP_WORDS[COMP_CWORD-1]}"
39
40 if [ ${prev} == -f ]; then
41 _filedir
42 else
43 # Search for the command
44 local skip_next=0
45 for ((i=1; $i<=$COMP_CWORD; i++)); do
46 if [[ ${skip_next} -eq 1 ]]; then
47 #echo "Skipping"
48 skip_next=0;
49 elif [[ ${COMP_WORDS[i]} != -* ]]; then
50 cmd="${COMP_WORDS[i]}"
51 cmd_index=${i}
52 break
53 elif [[ ${COMP_WORDS[i]} == -f ]]; then
54 skip_next=1
55 fi
56 done
57
58 # Search for the last option command
59 skip_next=0
60 for ((i=1; $i<=$COMP_CWORD; i++)); do
61 if [[ ${skip_next} -eq 1 ]]; then
62 #echo "Skipping"
63 skip_next=0;
64 elif [[ ${COMP_WORDS[i]} == -* ]]; then
65 option="${COMP_WORDS[i]}"
66 option_index=${i}
67 if [[ ${COMP_WORDS[i]} == -- ]]; then
68 break;
69 fi
70 elif [[ ${COMP_WORDS[i]} == -f ]]; then
71 skip_next=1
72 fi
73 done
74
75 if [[ $COMP_CWORD -le $cmd_index ]]; then
76 # The user has not specified a command yet
77 local all_commands="$(tmux -q list-commands | cut -f 1 -d ' ')"
78 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${all_commands}" -- "${cur}") )
79 else
80 case ${cmd} in
81 attach-session|attach)
82 case "$prev" in
83 -t) _tmux_complete_session "${cur}" ;;
84 *) options="-t -d" ;;
85 esac ;;
86 detach-client|detach)
87 case "$prev" in
88 -t) _tmux_complete_client "${cur}" ;;
89 *) options="-t" ;;
90 esac ;;
91 lock-client|lockc)
92 case "$prev" in
93 -t) _tmux_complete_client "${cur}" ;;
94 *) options="-t" ;;
95 esac ;;
96 lock-session|locks)
97 case "$prev" in
98 -t) _tmux_complete_session "${cur}" ;;
99 *) options="-t -d" ;;
100 esac ;;
101 new-sesison|new)
102 case "$prev" in
103 -t) _tmux_complete_session "${cur}" ;;
104 -@(n|d|s)) options="-d -n -s -t --" ;;
105 *)
106 if [[ ${COMP_WORDS[option_index]} == -- ]]; then
107 _command_offset ${option_index}
108 else
109 options="-d -n -s -t --"
110 fi
111 ;;
112 esac
113 ;;
114 refresh-client|refresh)
115 case "$prev" in
116 -t) _tmux_complete_client "${cur}" ;;
117 *) options="-t" ;;
118 esac ;;
119 rename-session|rename)
120 case "$prev" in
121 -t) _tmux_complete_session "${cur}" ;;
122 *) options="-t" ;;
123 esac ;;
124 source-file|source) _filedir ;;
125 has-session|has|kill-session)
126 case "$prev" in
127 -t) _tmux_complete_session "${cur}" ;;
128 *) options="-t" ;;
129 esac ;;
130 suspend-client|suspendc)
131 case "$prev" in
132 -t) _tmux_complete_client "${cur}" ;;
133 *) options="-t" ;;
134 esac ;;
135 switch-client|switchc)
136 case "$prev" in
137 -c) _tmux_complete_client "${cur}" ;;
138 -t) _tmux_complete_session "${cur}" ;;
139 *) options="-l -n -p -c -t" ;;
140 esac ;;
141
142 send-keys|send)
143 case "$option" in
144 -t) _tmux_complete_window "${cur}" ;;
145 *) options="-t" ;;
146 esac ;;
147 esac # case ${cmd}
148 fi # command specified
149 fi # not -f
150
151 if [[ -n "${options}" ]]; then
152 COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${options}" -- "${cur}") )
153 fi
154
155 return 0
156
157}
158complete -F _tmux tmux
159
160# END tmux completion
161