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