custom bash completion with whitespace and paths

561 views Asked by At

I can't figure out what I am doing wrong. I have my bash_completion file setup as such:

_bcd()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "$(back_directory.pl --complete $cur)" -- $cur) )
}
complete -o filenames -o nospace -F _bcd bcd

back_directory.pl is a program that will return directories paths up the tree: back_directory.pl --complete Th produces: This\ test/ But:

22:50:24-Josh@Joshuas-MacBook-Air:~/Desktop/bcd/This test/more    white/t$ bcd Th<TAB><TAB>
This   test/

As shown above, it doesn't auto complete for directories with whitespace in them (but it shows the completion option).

It should look like this: bcd This\ test/

I thought -o filenames should add the backslashes to escape the whitespace. Thanks for any help :)

2

There are 2 answers

5
chepner On BEST ANSWER

Your single call to compgen produces a single word (containing embdedded newlines), so you are only adding a single possible completion to COMPREPLY. Instead, you need to process the output of back_directory.pl one item at a time. Each item is tested as a possible match, and if compgen returns a non-empty string, add that to COMPREPLY.

_bcd() {
    local cur=${COMP_WORDS[COMP_CWORD]}
    IFS=: read -a matches < <(back_directory.pl --complete "$cur")
    for match in "${matches[@]}"; do
        possible=$(IFS= compgen -W "$match" -- "$cur")
        [[ $possible ] && COMPREPLY+=( "$possible" )
    done
}

(Note: I'm assuming back_directory.pl will produce a single line of output similar to

directory1:directory two:directory three:directory4

)

0
rottweilers_anonymous On

For the sake of completion, this is the final file:

_bcd()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    IFS=: read -a matches < <(back_directory.pl --complete "$cur")
    for match in "${matches[@]}"; do
        possible=$(IFS= compgen -W "$match" -- "${cur#/}")
        [[ $possible ]] && COMPREPLY+=( "$possible" )
    done

    longest=""
    for e in "${COMPREPLY[@]}"; do
        if [[ "$longest" == "" ]]; then
            longest="$e"
        fi
        while [[ ! "$e" =~ ^$longest ]]; do
            longest=${longest%?}
        done
    done

    if [[ $longest != "$input" && "$cur" =~ ^/ ]]; then
        for ((i=0; i<${#COMPREPLY[@]}; i++))
        do
            COMPREPLY[$i]="/${COMPREPLY[$i]}"
        done
    fi
}
complete -o filenames -o nospace -F _bcd bcd

The script back_directory.pl --complete will return a single line of paths delimited by colons.

My solution seems to be pretty terrible but it works.

Basically it removes a beginning slash from the current word, creates all the matches (containing no beginning slash) and then checks to see whether $longest is different from $input, which would mean bash would change your current word to something different--in which case we add back a beginning slash.