Search code examples
gitcompletion

git advanced alias completion


I have advanced alias's in my .gitconfig file that I would like to support tab completion.

For example, this simple alias allows for tab completion of refs (assuming the git completions have been sourced):

[alias]
  co = checkout

However, this does not:

[alias]
  co = "!f() { git checkout \"${@}\"; }; f}"

Is there any way to add support for tab completion to these alias's?


Solution

  • It turns out that it has been possible to add tab-completion support to a "complex alias" since at least git v2.1.0. Per git-completion.bash:

    # If you use complex aliases of form '!f() { ... }; f', you can use the null
    # command ':' as the first command in the function body to declare the desired
    # completion style.  For example '!f() { : git commit ; ... }; f' will
    # tell the completion to use commit completion.  This also works with aliases
    # of form "!sh -c '...'".  For example, "!sh -c ': git commit ; ... '".
    

    Therefore, this works:

      co = "!f() { : git checkout ; git checkout \"${@}\"; }; f}"
    

    Note that the pattern, !f is significant. This does not work: ! f (inserted space, although the name of the function is not significant).