mgalgs / fuzzy_bash_completion

Fuzzy tab completion for bash.
94 stars 9 forks source link

cd completion lists all files, not just directories #13

Open PlasmaHH opened 5 years ago

PlasmaHH commented 5 years ago

With "plain" bash completion, the candidates are filtered by being a directory, however this extension also displays files (in an empty directory the behaviour differs too btw.).

I have been playing myself with getting a zsh like fuzzy completion and came up with the following based on fzf (which is far too fuzzy). The goal is to have a/b/e expand to alice/bob/eve/ ... maybe you can draw some ideas from it. The main way to filter for directories here is using ls -d ${pattern}/ ... the / at the end causes ls to "filter" for directories only, the -d causes ls to show the directory instead of its contents.

_XD( )
{
        cur=${COMP_WORDS[COMP_CWORD]}
        ARGNUM="${#COMP_WORDS[@]}"

        COMPREPLY=()

        PAT=$cur
        #set -x
        declare -a PREFIX=( "./" )
        while read COMP;
        do
                NEWPREFIXES=( )

                for PRE in "${PREFIX[@]}";
                do
                PRE=${PRE#./}
                MATCHES=$( ls -d ${PRE}*/ 2>/dev/null | fzf -f "${PRE}${COMP}")
                NEWPREFIXES=( ${NEWPREFIXES[@]} ${MATCHES} )
                done

                PREFIX=( ${NEWPREFIXES[@]} )
        done < <(echo $PAT | sed 's/\//\n/g')

        COMPREPLY=( ${NEWPREFIXES[@]} )

        return 0
}