Eitan Adler | 3fc60b5 | 2012-04-17 00:24:58 -0400 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Travis Swicegood | f6d9325 | 2012-02-11 15:39:40 -0600 | [diff] [blame] | 2 | |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 3 | # 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 Welch | e2e63c9 | 2012-06-30 16:47:13 -0400 | [diff] [blame] | 8 | _tmux_cmds=" \ |
| 9 | attach-session \ |
| 10 | bind-key \ |
| 11 | break-pane \ |
| 12 | capture-pane \ |
| 13 | choose-client \ |
| 14 | choose-session \ |
| 15 | choose-window \ |
| 16 | clear-history \ |
| 17 | clock-mode \ |
| 18 | command-prompt \ |
| 19 | confirm-before \ |
| 20 | copy-buffer \ |
| 21 | copy-mode \ |
| 22 | delete-buffer \ |
| 23 | detach-client \ |
| 24 | display-message \ |
| 25 | display-panes \ |
| 26 | down-pane \ |
| 27 | find-window \ |
| 28 | has-session \ |
| 29 | if-shell \ |
| 30 | join-pane \ |
| 31 | kill-pane \ |
| 32 | kill-server \ |
| 33 | kill-session \ |
| 34 | kill-window \ |
| 35 | last-window \ |
| 36 | link-window \ |
| 37 | list-buffers \ |
| 38 | list-clients \ |
| 39 | list-commands \ |
| 40 | list-keys \ |
| 41 | list-panes \ |
| 42 | list-sessions \ |
| 43 | list-windows \ |
| 44 | load-buffer \ |
| 45 | lock-client \ |
| 46 | lock-server \ |
| 47 | lock-session \ |
| 48 | move-window \ |
| 49 | new-session \ |
| 50 | new-window \ |
| 51 | next-layout \ |
| 52 | next-window \ |
| 53 | paste-buffer \ |
| 54 | pipe-pane \ |
| 55 | previous-layout \ |
| 56 | previous-window \ |
| 57 | refresh-client \ |
| 58 | rename-session \ |
| 59 | rename-window \ |
| 60 | resize-pane \ |
| 61 | respawn-window \ |
| 62 | rotate-window \ |
| 63 | run-shell \ |
| 64 | save-buffer \ |
| 65 | select-layout \ |
| 66 | select-pane \ |
| 67 | select-prompt \ |
| 68 | select-window \ |
| 69 | send-keys \ |
| 70 | send-prefix \ |
| 71 | server-info \ |
| 72 | set-buffer \ |
| 73 | set-environment \ |
| 74 | set-option \ |
| 75 | set-window-option \ |
| 76 | show-buffer \ |
| 77 | show-environment \ |
| 78 | show-messages \ |
| 79 | show-options \ |
| 80 | show-window-options \ |
| 81 | source-file \ |
| 82 | split-window \ |
| 83 | start-server \ |
| 84 | suspend-client \ |
| 85 | swap-pane \ |
| 86 | swap-window \ |
| 87 | switch-client \ |
| 88 | unbind-key \ |
| 89 | unlink-window \ |
| 90 | up-pane" |
| 91 | |
Jon Schewe | 9b866fa | 2012-02-12 12:30:07 -0600 | [diff] [blame] | 92 | _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 Schewe | 98c9bc1 | 2012-02-12 12:14:48 -0600 | [diff] [blame] | 106 | _tmux_filedir () |
| 107 | { |
| 108 | local IFS=' |
| 109 | '; |
Jon Schewe | 9b866fa | 2012-02-12 12:30:07 -0600 | [diff] [blame] | 110 | _tmux_expand || return 0; |
Jon Schewe | 98c9bc1 | 2012-02-12 12:14:48 -0600 | [diff] [blame] | 111 | 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 Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 118 | function _tmux_complete_client() { |
| 119 | local IFS=$'\n' |
| 120 | local cur="${1}" |
Von Welch | e2e63c9 | 2012-06-30 16:47:13 -0400 | [diff] [blame] | 121 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-clients 2>/dev/null | cut -f 1 -d ':')" -- "${cur}") ) |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 122 | } |
| 123 | function _tmux_complete_session() { |
| 124 | local IFS=$'\n' |
| 125 | local cur="${1}" |
Von Welch | e2e63c9 | 2012-06-30 16:47:13 -0400 | [diff] [blame] | 126 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-sessions 2>/dev/null | cut -f 1 -d ':')" -- "${cur}") ) |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 127 | } |
| 128 | function _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 Welch | e2e63c9 | 2012-06-30 16:47:13 -0400 | [diff] [blame] | 134 | sessions="$(tmux -q list-sessions 2>/dev/null | sed -re 's/([^:]+:).*$/\1/')" |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 135 | if [[ -n "${session_name}" ]]; then |
| 136 | sessions="${sessions} |
Von Welch | e2e63c9 | 2012-06-30 16:47:13 -0400 | [diff] [blame] | 137 | $(tmux -q list-windows -t "${session_name}" 2>/dev/null | sed -re 's/^([^:]+):.*$/'"${session_name}"':\1/')" |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 138 | 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 Schewe | 98c9bc1 | 2012-02-12 12:14:48 -0600 | [diff] [blame] | 153 | _tmux_filedir |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 154 | 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 Welch | e2e63c9 | 2012-06-30 16:47:13 -0400 | [diff] [blame] | 189 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${_tmux_cmds}" -- "${cur}") ) |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 190 | 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 Swicegood | f6d9325 | 2012-02-11 15:39:40 -0600 | [diff] [blame] | 212 | new-session|new) |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 213 | case "$prev" in |
| 214 | -t) _tmux_complete_session "${cur}" ;; |
Jon Schewe | 92c9759 | 2012-02-11 14:48:09 -0600 | [diff] [blame] | 215 | -[n|d|s]) options="-d -n -s -t --" ;; |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 216 | *) |
| 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 Schewe | 98c9bc1 | 2012-02-12 12:14:48 -0600 | [diff] [blame] | 235 | source-file|source) _tmux_filedir ;; |
Jon Schewe | 9e80864 | 2012-02-10 21:01:10 -0600 | [diff] [blame] | 236 | 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 | } |
| 269 | complete -F _tmux tmux |
| 270 | |
| 271 | # END tmux completion |
| 272 | |