在bash别名中Git自动完成?

Cla*_*dio 60 git bash

我使用的go是一个简单的bash别名git checkout branchname.我想念的是自动完成功能,它与完整git checkout branchna...命令一起使用,但不在别名中.

有没有办法指示Bash"继承"另一个命令的自动完成"驱动程序"?

小智 76

使用后complete -F:

complete -F _git_checkout go
Run Code Online (Sandbox Code Playgroud)

之后的标签go可能会导致:

bash: [: 1: unary operator expected
Run Code Online (Sandbox Code Playgroud)

而不是complete,使用__git_complete

这是git bash completion的内置函数.

声明别名后,将正确的自动完成功能绑定到它:

alias g="git"
__git_complete g _git

alias go="git checkout"
__git_complete go _git_checkout

alias gp="git push"
__git_complete gp _git_push
Run Code Online (Sandbox Code Playgroud)

  • 运行时我得到了`__git_complete:command not found` (5认同)
  • @LưuVĩnhPhúc我遇到了同样的问题.这是因为我的bash-completion包中有`__git_complete`函数(通过自制的'brew install bash-completion`安装),这个函数目前不在我的bash配置文件中.不得不将此行添加到〜/ .bash_profile:`[ - f/usr/local/etc/bash_completion] &&.在/ usr/local/etc中/ bash_completion` (3认同)
  • 它应该是`__git_main`,而不是`_git`。`_git` 存在的原因只是为了向后兼容,它基本上是 `__git_main` 包装的。 (2认同)

Sha*_*hin 39

如果您可以找到原始命令使用的完成功能,则可以使用它将其分配给别名complete -F.

例如,在我的ubuntu框中,git checkoutis 使用的完成函数是_git_checkout(找到/etc/bash_complete.d/git).

在运行之前complete -F:

[me@home]$ git checkout <TAB><TAB>
HEAD            master          origin/HEAD     origin/master

[me@home]$ alias go="git checkout"

[me@home]$$ go <TAB><TAB>
.git/                precommit_config.py  README.md            SvnSentinel/         
.gitignore           precommit.py         startcommit.py       tests/ 
Run Code Online (Sandbox Code Playgroud)

后:

[me@home]$$ complete -F _git_checkout go

[me@home]$$ go <TAB><TAB>
HEAD            master          origin/HEAD     origin/master 
Run Code Online (Sandbox Code Playgroud)

  • 如果`complete -F`不适合你,请参阅http://stackoverflow.com/a/24665529/3779下的`__git_complete`答案 (6认同)

Sea*_*usJ 12

在Ubuntu 16.04.3 LTS中,我需要提供的文件是/usr/share/bash-completion/completions/git.所以在.bash_custom(或.bashrc,无论如何):

[ -f /usr/share/bash-completion/completions/git ] && . /usr/share/bash-completion/completions/git
__git_complete g __git_main
Run Code Online (Sandbox Code Playgroud)

(这个答案属于对程序员的回复jangosteve的评论,但由于我需要50个代表来创建评论,我会将其作为自己的答案进行垃圾邮件.)


ada*_*ish 10

在Ubuntu 18.04(Bionic)上,以下工作正常。加入这样的片段(与你的别名)为您的首选bash的配置文件,例如.bashrc.bash_aliases .bash_profile

# define aliases
alias gc='git checkout'
alias gp='git pull'

# setup autocompletion
if [ -f "/usr/share/bash-completion/completions/git" ]; then
  source /usr/share/bash-completion/completions/git
  __git_complete gc _git_checkout
  __git_complete gp _git_pull
else
  echo "Error loading git completions"
fi
Run Code Online (Sandbox Code Playgroud)

通常,__git_complete伪指令的格式如下:

__git_complete <YOUR ALIAS> _git_<GIT COMMAND NAME>
Run Code Online (Sandbox Code Playgroud)

这将现有答案中的智慧整合到一个最新的答案中,谢谢大家。

  • 2021 年 8 月仍可在 Ubuntu 20.04 中使用 (2认同)

Fel*_*peC 5

正如其他人回答的那样,您应该使用__git_complete,否则脚本将失败。

alias g="git"
__git_complete g __git_main

alias g="gl"
__git_complete gl _git_log
Run Code Online (Sandbox Code Playgroud)

但你不应该使用_git主命令,它是__git_main.

不幸的是,隐藏了很多有关完成的信息,但您可以在我的分支的自述文件中找到更多信息:git-completion