3

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?

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • what you may need is [https://github.com/git/git/blob/master/contrib/completion/git-completion.bash](https://github.com/git/git/blob/master/contrib/completion/git-completion.bash)? – Yi Tang Jun 19 '18 at 07:53
  • @YiTang hmm... I don't understand how to use this.... I already have git installed and it has tab completion so I think this is already sourced somewhere - bit that is for `git` command. How would I get it to work for my `all-git` command? – code_fodder Jun 19 '18 at 08:31

2 Answers2

4

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:

hradecek
  • 2,455
  • 2
  • 21
  • 30
  • wow....sweet! - that just works. I don't quite get your comment about "the function used to complete git 'submodules' is called _git". I tested this and it seems to do auto complete for all git functions not just git submodules... or did I miss understand!?...anyway - that is so awesome you deserve +85! – code_fodder Jun 19 '18 at 11:05
  • Sorry - I have marked up the answer +25, but I have to wait for 19 more hours until I can mark up the bounty : ( ... if I forget (which I won't) but just in case ping me a message tomorrow!! – code_fodder Jun 19 '18 at 11:06
  • Glad to help! Answer to `_git` related question: Basically we are using/calling a `_git` function from `git-completion.sh`. No worries :) – hradecek Jun 19 '18 at 11:30
  • ah ha... plus 50 sir, well deserved : ) – code_fodder Jun 20 '18 at 06:14
  • You should not be using `_git` directly, it's better to use `__git_complete`. – FelipeC Jun 25 '19 at 15:24
2

It's very simple:

source /usr/share/bash-completion/completions/git
__git_complete all-git.sh __git_main

The __git_complete() function is a helper to create Git completions, it's used by Git itself like this: __git_complete git __git_main. You can use it for specific commands, like __git_complete all-submodule.sh _git_submodule.

It's specifically designed for this, so no need to complicate it more.

FelipeC
  • 9,123
  • 4
  • 44
  • 38