#StackBounty: #linux #command-line #bash #shell #shell-script How can I get bash command line completion to complete a partial argument…

Bounty: 50

Imagine I have a command ‘command’ which takes arguments foo:bar foo:foo bar:bar or bar:foo
I can write a command line completion script that I can source into my environment to give me completion that looks like the following:

function _command_opt()
    local cur prev opts
    opts="foo:bar foo:foo bar:bar bar:foo"
    if [[ ${prev: -7} == 'command' ]]
        COMPREPLY=( $(compgen -W "${opts}"  -- ${cur}) );
        return 0;
complete -F _command_opt -o bashdefault -o default 'command'

However if I type command f[tab][tab] I get foo:bar and foo:foo listed, but neither is completed onto my command line (because they aren’t yet distinct. What I would like to accomplish is that this produces command foo: and leaves my cursor at the end of the current word (rather than adding a space) to allow me to then type b or f and then tab again to finish the command. The example problem is just to show what I mean… the real case the prefix:suffix combinations are quite long, so partial completion would be really beneficial. e.g. 25 different commands that all begin with prefix translationupdateprocess: and translationupdateprocess is one of 25 different equally long prefixs with that many options.

Does anyone know of a way to get bash complete to operate this way?

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.