1

I have followed this tutorial to write my bash completion function. It has worked out just fine, but I was wondering if it is possible to separately show completions from different sources.

For example, suppose I have a script that takes a directory as argument and I want to autocomplete the possible directories (listed by get_dirs):

function _testing_command()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}

    case "$prev" in
        testing )
            COMPREPLY=( $(compgen -W "$(get_dirs)" -- $cur) )
            ;;
    esac
}
complete -F _testing_command testing

This works fine and autocompletes the directories correctly:

$ testing <TAB><TAB>
dir1 dir3 dir5
$ testing

However, what if I now have a second set of directories (listed by get_dirs_2) that I want to include in the autocomplete output. I can do the following:

function _testing_command()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}

    case "$prev" in
        testing )
            COMPREPLY=( $(compgen -W "$(get_dirs) $(get_dirs_2)" -- $cur) )
            ;;
    esac
}
complete -F _testing_command testing

Which will give me this output:

$ testing <TAB><TAB>
dir1 dir2 dir3 dir4 dir5 dir6
$ testing

Technically this is ok, because autocompletion will work on the directories from both sources. But I want the output to somehow signify which directory comes from which location, for example:

$ testing <TAB><TAB>
from loc1:
dir1 dir3 dir5
from loc2:
dir2 dir4 dir6
$ testing

Do I have any control over the output format? Or is it just not possible to achieve something like this with the complete/compgen functions?

  • I don't think it's possible in bash. Zsh can do it, it gives you a lot of control over how completions are displayed. – Gilles Oct 8 '15 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.