git 高级别名补全

sco*_*ott 5 git completion

我的 .gitconfig 文件中有高级别名,我想支持制表符完成。

例如,这个简单的别名允许对 refs 进行 Tab 补全(假设已获取 git 补全):

[alias]
  co = checkout
Run Code Online (Sandbox Code Playgroud)

然而,这并不:

[alias]
  co = "!f() { git checkout \"${@}\"; }; f}"
Run Code Online (Sandbox Code Playgroud)

有什么方法可以为这些别名添加对制表符完成的支持吗?

sco*_*ott 5

事实证明,至少从 git v2.1.0 开始,就可以为“复杂别名”添加制表符完成支持。根据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 ; ... '".
Run Code Online (Sandbox Code Playgroud)

因此,这有效:

  co = "!f() { : git checkout ; git checkout \"${@}\"; }; f}"
Run Code Online (Sandbox Code Playgroud)

请注意,该模式!f很重要。这不起作用:(! f插入空格,尽管函数名称并不重要)。