Skip to content

Instantly share code, notes, and snippets.

@paulelms
Forked from ttscoff/up.completion.bash
Created June 13, 2014 20:36
Show Gist options
  • Save paulelms/9eb38451a0c9b9ec338f to your computer and use it in GitHub Desktop.
Save paulelms/9eb38451a0c9b9ec338f to your computer and use it in GitHub Desktop.
#!/bin/bash
# Bash completion for `up` <http://brettterpstra.com/2014/05/14/up-fuzzy-navigation-up-a-directory-tree/>
_up_complete()
{
local rx
local token=${COMP_WORDS[$COMP_CWORD]}
local IFS=$'\t'
local words=$(dirname `pwd` | tr / " ")
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 _up_complete up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment