Search code examples
bashgitubuntutab-completion

Creating a tab completion script. In one case I want to use the completion for another command (e.g. git), is it possible?


So I have some commands I am scripting up:

all-build.sh all-git.sh

These run commands on multiple folders and spawn output windows (running tail -f on logs) and all sorts of other things.

However I they each take different commands and I want tab completion

So I read a tutorial on tab completion and I have the all-completion.bash script as shown here:

#!/usr/bin/env bash

complete -W "param1 param2 param3" all-build.sh
complete -W "status log push pull" all-git.sh

As you can see the all-build auto complete params are my own custom ones and I am happy with that. However the git ones are a repeat of some of the actual git parameters. So really what I want is something like:

complete <use git completion> all-git.sh

So that when I do all-git submodule upd<tab> it auto completes to all-git submodule update using git's auto complete.

Is that possible?


Solution

  • We might use _completion_loader, (which is used to define completions on the fly) in order to load git completions.

    Then we might observe in official git-completion.sh file, that function used to complete git "submodules" is called _git, so we can simply pass it to complete via -F <function> option, see complete documentation.

    So by writing

    source /usr/share/bash-completion/bash_completion
    _completion_loader git
    
    complete -F _git all-git.sh
    

    we can achieve desired behaviour (if I got your issue correctly).

    Note: That bash_completion script, might be located on your machine in different folder. I've tested it on Ubuntu 16.04 with bash 4.3.48.

    EDIT: In case this doesn't fully solve your issue, I believe this is the way you want to go :). You can pretty much bend the solution for your needs.

    EDIT-2: I've also assumed, you have already installed and working git completions.

    EDIT-3: I've found a few interesting links, which are not solving your particular issue, but can give some light and deeper knowledge: