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