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