8

I have a list of words that should be present in the order of appearance in completion to a certain key, but bash seems to be internally sorting what is assigned to COMPREPLY array. How to avoid that?

Example:

_comm() {
    _init_completion -s -n : || return
    case $prev in
        -a) COMPREPLY=(zxy abcdef tyuu fgsfds)
            ;;
    esac
}
complete -F _comm comm

If you run this code, bash should complete

$ comm -a 

with

abcdef fgsfds tyuu zxy

i.e. sorted alphabetically.

2 Answers 2

10

Since Bash 4.4 you can use nosort option. In your example change the last line to:

complete -o nosort -F _comm comm

and you should get completions without alphabetical sorting.

Important note: options (specified with -o) must precede functions (-F). That’s why coderofsalvation’s code didn’t work.

1

This should be achieved by modifying the complete command:

complete -F _comm comm

into:

complete -F _comm comm -o nosort 

As mentioned in the documentation:

however I didn't get this to work on bash 4 :/

It seems it is a documented, yet not implemented feature of controlling readline's variable rl_sort_completion_matches.

More info

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .