bash bash别名中的Git自动完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9869227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git autocomplete in bash aliases?
提问by Claudio
I'm using goas a simple bash alias for git checkout branchname. The thing that I miss is the autocomplete feature that works with the full git checkout branchna...command, but not in the alias.
我go用作 .bash 的简单 bash 别名git checkout branchname。我想念的是与完整git checkout branchna...命令一起使用但不在别名中的自动完成功能。
Is there a way to instruct Bash to "inherit" the autocomplete "driver" for another command?
有没有办法指示 Bash 为另一个命令“继承”自动完成“驱动程序”?
采纳答案by Shawn Chin
If you can find out the completion function used by the original command, you can assign it to the alias using complete -F.
如果你能找出原命令使用的补全函数,你可以使用complete -F.
For example, on my ubuntu box, the completion function used by git checkoutis _git_checkout(found in /etc/bash_complete.d/git).
例如,在我的 ubuntu 盒子上,使用的完成函数git checkout是_git_checkout(在 中找到/etc/bash_complete.d/git)。
Example
例子
Before running complete -F:
运行前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/
After:
后:
[me@home]$$ complete -F _git_checkout go
[me@home]$$ go <TAB><TAB>
HEAD master origin/HEAD origin/master
回答by theprogrammerin
After using complete -F:
使用后complete -F:
complete -F _git_checkout go
Tabbing after gomay result in:
在之后的选项卡go可能会导致:
bash: [: 1: unary operator expected
Instead of complete, use __git_complete
而不是complete,使用__git_complete
This is git bash completion's built-in function for this purpose.
这是用于此目的的 git bash 完成的内置函数。
After declaring your alias, bind the correct auto-complete function to it:
声明别名后,将正确的自动完成功能绑定到它:
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
回答by SeamusJ
In Ubuntu 16.04.3 LTS, the file I needed to source was /usr/share/bash-completion/completions/git. So in .bash_custom(or .bashrc, whatever):
在 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
回答by adamnfish
On Ubuntu 18.04 (Bionic) the following works. Add something like this snippet (with your aliases) to your preferred bash configuration file e.g. .bashrc, .bash_aliases.bash_profile.
在 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
In general the format of the __git_completedirective is the following:
一般来说,__git_complete指令的格式如下:
__git_complete <YOUR ALIAS> _git_<GIT COMMAND NAME>
This combines wisdom from the existing answers in a single up-to-date answer, thank you all.
这将现有答案的智慧结合在一个最新的答案中,谢谢大家。
回答by yugr
To add to other excellent answers: normally you have a lot of Git aliases and it may be tedious to manually forward completions for all of them. Here's a small trick to do this automatically:
添加其他优秀答案:通常您有很多 Git 别名,手动转发所有这些别名可能很乏味。这是自动执行此操作的一个小技巧:
if [ -f "/usr/share/bash-completion/completions/git" ]; then
# Enable Git completions for aliases
. /usr/share/bash-completion/completions/git
for a in $(alias | sed -n 's/^alias \(g[^=]*\)=.git .*//p'); do
c=$(alias $a | sed 's/^[^=]*=.git \([a-z0-9\-]\+\).*//' | tr '-' '_')
if set | grep -q "^_git_$c *()"; then
eval "__git_complete $a _git_$c"
fi
done
fi
回答by Anthony Kong
For macOS, run the following to install bash completion
对于macOS,运行以下命令来安装 bash 完成
brew install bash-completion
Then add the following
然后添加以下内容
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
to your .bashrc or .bash_profile
到您的 .bashrc 或 .bash_profile
回答by Mario Olivio Flores
On Linux Mint, this didn't work for me. I was getting bash: [: 1: unary operator expected
.
在 Linux Mint 上,这对我不起作用。我得到了bash: [: 1: unary operator expected
。
I found the following response worked quite well - with the troubleshooting section the user provided to be quite helpful. https://superuser.com/questions/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases
我发现以下响应效果很好 - 用户提供的故障排除部分非常有帮助。 https://superuser.com/questions/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases

