Say I have a git-cc
executable in PATH
. If git-cc
supports --help
, then it is easy to provide adequate completion with
complete -F _longopt git-cc
This makes $ git-cc --<TAB>
complete (as per help output). But git cc --<TAB>
won't complete (even though it runs fine). More importantly if I create a git alias to the custom subcommand, e.g. cc-sensible-defaults = cc --opt1 ...
, that also won't work, and in this case simply deleting the space (git-cc
instead of git cc
) isn't an option.
What to do? I've tried messing around with __git_complete [git-]cc _longopt
, but none of the various combinations do anything good. It seems to be for completing bash aliases (like gl = git-log
), not sub-commands. The intro in git/git-completion.bash is, as expected, not very helpful, containing the confusing
# If you have a command that is not part of git, but you would still
# like completion, you can use __git_complete:
#
# __git_complete gl git_log
#
# Or if it's a main command (i.e. git or gitk):
#
# __git_complete gk gitk
(WTH is _git_log? Did they mean _git_log, which is indeed a function? Is it some convention?)
Update:
What you have to do, is remove from
COMPREPLY
the words so that only one that starts with$cur
is left. If you give 3, bash will show you the list. If you reduceCOMPREPLY=(diff)
then it will be auto-completed by Bash.Taking inspiration of https://github.com/git/git/blob/master/contrib/completion/git-completion.bash#L2445 , the following works nicely:
Or I think it's better to write similar code yourself, not to depend on
git
:For me https://devmanual.gentoo.org/tasks-reference/completion/index.html is the best introduction how to do it.
Just define a function that does the compiletion with leading
_git_
prefix.See __git_complete_command:
As far as I understand the
__git_complete
it's the other way round - you want a normal command complete like git subcommand. For example:_git_log
is a function the generates completion forgit log
.Yes. See __git_complete tests for existence of a function with
_main
suffix or_
prefix or without any prefix/suffix.Yes.