简体   繁体   English

Git选项卡完成在Mac上的zsh中不起作用

[英]Git tab completion not working in zsh on mac

No matter what I try and do I can't seem to make git tab/auto completion work in my zsh shell.无论我尝试做什么,我似乎都无法在我的 zsh shell 中使 git tab/auto 完成工作。 I've downloaded the bash-completion script and the zsh-completion one and followed the instructions, but I can't make it work.我已经下载了 bash-completion 脚本和 zsh-completion 一个并按照说明进行了操作,但我无法使其工作。

I've reinstalled oh-my-zsh but that didn't seem to help or make any difference.我已经重新安装了 oh-my-zsh,但这似乎没有帮助或有任何区别。

Can anyone who's got it working describe to me their setup so I can try an emulate it to get it working for me?任何让它工作的人都可以向我描述他们的设置,这样我就可以尝试模拟它来让它为我工作吗?

To be specific, what I've done so far is:具体来说,到目前为止我所做的是:

No luck.没运气。

Actually, ZSH does know how to do git completion out of the box, but you need to turn on the completion feature itself (which from the steps you described I guess you haven't done)实际上,ZSH确实知道如何开箱即用地完成 git 完成,但是您需要自己打开完成功能(从您描述的步骤中我猜您还没有完成)

Adding this to your .zshrc should be enough:将此添加到您的.zshrc应该足够了:

autoload -Uz compinit && compinit

After you put the line .zshrc file, don't forget to restart the shell for ZSH to pick up the new config (alternatively, you can execute the line in your current session, that'll enable autocompletion for that one session)你放线后.zshrc文件,不要忘记重启外壳为ZSH拿起新的配置(或者,您可以在当前会话中执行的线,那将启用一个会话自动完成)

Here's a one-liner to both add the line and apply to current session:这是添加行并应用于当前会话的单行:

echo 'autoload -Uz compinit && compinit' >> ~/.zshrc && . ~/.zshrc

For more info see here https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Zsh有关更多信息,请参见此处https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Zsh

PS Another answer here suggests installing the hub tool instead: although the tool is handy, it's merely a 3rd party (github community) wrapper around git. PS 这里的另一个答案建议改为安装hub工具:虽然该工具很方便,但它只是 git 的第 3 方(github 社区)包装器。 Hence, it has nothing to do with the topic of "Git completion in ZSH"因此,它与“ZSH中的Git完成”主题无关

For the 2019 viewer:对于 2019 年观众:

If you use ZSH:如果您使用 ZSH:

brew install hub

mkdir ~/.zsh and mkdir ~/.zsh/completions mkdir ~/.zshmkdir ~/.zsh/completions

Once you got your directory created and hub installed , you have to cp the hub.bash_completion.sh file to your local zsh/completion folder.创建目录安装集线器后,您必须将hub.bash_completion.sh文件cp到本地zsh/completion文件夹。
(Mine was cp /usr/local/etc/bash_completion.d/hub.bash_completion.sh ~/.zsh/completions/_hub ) (我的是cp /usr/local/etc/bash_completion.d/hub.bash_completion.sh ~/.zsh/completions/_hub

Then you add the following line to your ~/.zshrc file :然后~/.zshrc下行添加到~/.zshrc文件中:

fpath=(~/.zsh/completions $fpath) 
autoload -U compinit && compinit

then source ~/.zshrc and voilà !然后source ~/.zshrc You should have the git completion available你应该有可用的 git 完成

source : https://github.com/github/hub/tree/master/etc#zsh来源: https : //github.com/github/hub/tree/master/etc#zsh

The answer was that I had alias git=hub in my .zshrc file.答案是我的.zshrc文件中有alias git=hub See https://github.com/github/hub for info on hub (it's awesome).有关集线器的信息,请参阅https://github.com/github/hub (很棒)。

Here's a link to info about the problem I was having with hub and git completion: https://github.com/github/hub/issues/586#issuecomment-47727226这是有关我在使用集线器和 git 完成时遇到的问题的信息的链接: https : //github.com/github/hub/issues/586#issuecomment-47727226

Turns out the problem for me wass that when installing git via homebrew, git installs its own zsh shell extension which is considerably less complete/capable than the default that oh-my-szh installs.对我来说,问题是当通过自制软件安装 git 时,git 安装了自己的 zsh shell 扩展,这比 oh-my-szh 安装的默认值要少得多。 Find out what versions your git install is and then remove the zsh autocompletions.找出你的 git install 是什么版本,然后删除 zsh 自动完成。 Mine were here and deleted thusly:我的在这里并因此删除:

rm -rf /usr/local/Cellar/git/2.28.0/share/zsh/

This is not to say that the problem could not be any one of the other answers or a combination of.这并不是说问题不可能是其他答案中的任何一个或组合。

The same issue bothers me today.今天同样的问题困扰着我。 Inspired by the answers, I looked around the .zshrc and found this:受到答案的启发,我环顾了 .zshrc 并发现了以下内容:

Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
Example format: plugins=(rails git textmate ruby lighthouse)
Add wisely, as too many plugins slow down shell startup.
plugins=(git)

Just comment the above line to enable the git plugin, the problem is solved.把上面这行注释掉,开启git插件,问题就解决了。 More details about the plugin can be found here: https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/git关于插件的更多细节可以在这里找到: https : //github.com/robbyrussell/oh-my-zsh/tree/master/plugins/git

如果没有任何帮助,请尝试使用 Homebrew brew install git /usr/local/share/zsh/site-functions/_git安装 git 后出现的符号链接/usr/local/share/zsh/site-functions/_git

mkdir -p ~/.zsh/completions
curl "https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh" > ~/.zsh/completions/_git
chmod a+x ~/.zsh/completions/_git
cat ~/.zshrc | grep 'fpath=(~/.zsh/completions $fpath)' > /dev/null || echo 'fpath=(~/.zsh/completions $fpath)' >> ~/.zshrc
cat ~/.zshrc | grep 'autoload -Uz compinit && compinit' > /dev/null || echo 'autoload -Uz compinit && compinit' >> ~/.zshrc
source ~/.zshrc

Or just run brew install zsh-completions and follow the instructions.或者只是运行brew install zsh-completions并按照说明进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM