Search code examples
bashcompletion

using bash_completion with cli tool


I have a custom cli tool that i want to setup with bash_completion but want [tab][tab] to perform an enter action on cmdline.

my bash_completion file for wonder is:

_wonder()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "audit nodes tools create debug delete update" -- $cur) )
}
complete -F _wonder wonder

currently:

$ wonder [tab] [tab]

returns:

ip-10-99-18-249:loco_dsl jasonparmar$ wonder
audit   create  debug   delete  nodes   tools   update

What I want is when i use the tools option from wonder:

$ wonder tools [tab] [tab] 

I want the [tab][tab] to force an enter on the cmdline

How can i edit my bash_completion file for wonder to achieve this.

Is this even possible with bash_completion.

Thanks in advance.


Solution

  • If you don't mind using external tools to simulate keyboard input (such as xdotool), try adding this as the first line of your completion function:

    (( COMP_CWORD > 1 )) && xdotool key Return
    

    As noted by @RandomUser, completion executing a command is unexpected behavior. See, for instance: echo oops; wonder audit <tab><tab> and imagine rm -rf * instead of echo. Consider simply stopping the completion to indicate wonder accepts only one argument:

    (( COMP_CWORD > 1 )) && return