Skip to content

Instantly share code, notes, and snippets.

@nnutter
Forked from ttscoff/tm.completion.bash
Last active August 29, 2015 14:01
Show Gist options
  • Save nnutter/a53a1623f8a61a7083bb to your computer and use it in GitHub Desktop.
Save nnutter/a53a1623f8a61a7083bb to your computer and use it in GitHub Desktop.
_tm_complete() {
local rx
local token=${COMP_WORDS[$COMP_CWORD]}
local IFS=$'\t'
local words
if [ $COMP_CWORD -eq 1 ]; then
words=$(tmux -q list-sessions 2> /dev/null | cut -f 1 -d ':' | tr "\n" " " | sed 's/ $//')
fi
if [ $COMP_CWORD -eq 2 ]; then
words=$(tmux list-windows -t ${COMP_WORDS[1]} 2> /dev/null | awk '{print $2}' | tr -d '*-' | tr "\n" " " | sed 's/ $//')
fi
local nocasematchWasOff=0
shopt nocasematch >/dev/null || nocasematchWasOff=1
(( nocasematchWasOff )) && shopt -s nocasematch
local w matches=()
if [[ $token == "" ]]; then
matches=($words)
else
for w in $words; do
rx=$(ruby -e "print '$token'.gsub(/\s+/,'').split('').join('.*')")
if [[ "$w" =~ $rx ]]; then
matches+=($w)
fi
done
fi
(( nocasematchWasOff )) && shopt -u nocasematch
COMPREPLY=("${matches[@]}")
}
complete -F _tm_complete tm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment