1

I am trying to create a bash completion for a custom function that I am creating:

gco() {
    if [ -z "$1" ]; then
        branch="$(git branch --all | fzf | tr -d '[:space:]')"
    else
        branch="$1"
    fi
    git checkout "$branch"
}

I want to be able to delegate the auto completion of my gco function to the auto completion for git checkout, so I tried adding this:

function _gco() {
    local curr_arg;
    curr_arg=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "$(git checkout)" -- $curr_arg ) );

}
complete -F _gco gco

However, when I type in gco and then press tab, I get the following output:

$ gco 
1           different     into                respectively.    yours)
38          diverged,     merge               the
and         each,         origin/mybranch     to
branch      git           pull                (use
commits     have          remote              Your

I think the issue is with this line:

COMPREPLY=( $(compgen -W "$(git checkout)" -- $curr_arg ) );

I don't know what the auto completion script is for git checkout, but I want to know if there is a way I can run the auto completion script for any arbitrary command without actually running said arbitrary command:

_run_auto_completion_script_for_command "git checkout"

Is there a way I can do this?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.