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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24513873/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:09:34  来源:igfitidea点击:

Git tab completion not working in zsh on mac

gitzshbash-completionoh-my-zshzsh-completion

提问by hamchapman

No matter what I try and do I can't seem to make git tab/auto completion work in my zsh shell. I've downloaded the bash-completion script and the zsh-completion one and followed the instructions, but I can't make it work.

无论我尝试做什么,我似乎都无法在我的 zsh shell 中使 git tab/auto 完成工作。我已经下载了 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.

没运气。

采纳答案by hamchapman

The answer was that I had alias git=hubin my .zshrcfile. See https://github.com/github/hubfor info on hub (it's awesome).

答案是alias git=hub我的.zshrc档案里有。有关集线器的信息,请参阅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

回答by Ivan Bartsov

Actually, ZSH doesknow 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 .zshrcshould be enough:

将此添加到您的.zshrc应该足够了:

autoload -Uz compinit && compinit

After you put the line .zshrcfile, 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 重新启动 shell 以获取新配置(或者,您可以在当前会话中执行该行,这将为该会话启用自动完成功能)

By using the following commands in a terminal, the above-mentioned command can easily be added to the ".zshrc" file even if there is no such a file and you do not need to restart the terminal.

通过在终端中使用以下命令,即使没有这样的文件并且您不需要重新启动终端,也可以轻松地将上述命令添加到“.zshrc”文件中。

echo 'autoload -Uz compinit && compinit' >> ~/.zshrc
source ~/.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 hubtool instead: although the tool is handy, it's merely a 3rd party (github community) wrapper around git. Hence, it has nothing to do with the topic of "Git completion in ZSH"

PS 此处的另一个答案建议hub改为安装该工具:尽管该工具很方便,但它只是 git 的第 3 方(github 社区)包装器。因此,它与“ZSH 中的 Git 完成”主题无关

回答by Djamel

For the 2019 viewer:

对于 2019 年观众:

If you use ZSH:

如果您使用 ZSH:

brew install hub

brew install hub

mkdir ~/.zshand mkdir ~/.zsh/completions

mkdir ~/.zshmkdir ~/.zsh/completions

Once you got your directory createdand hub installed, you have to cpthe hub.bash_completion.shfile to your local zsh/completionfolder.
(Mine was cp /usr/local/etc/bash_completion.d/hub.bash_completion.sh ~/.zsh/completions/_hub)

一旦你有你的目录中创建安装集线器,您必须cphub.bash_completion.sh文件到本地zsh/completion文件夹中。
(我的是cp /usr/local/etc/bash_completion.d/hub.bash_completion.sh ~/.zsh/completions/_hub

Then you add the following line to your ~/.zshrcfile :

然后将以下行添加到~/.zshrc文件中:

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

then source ~/.zshrcand voilà! You should have the git completion available

然后source ~/.zshrc!你应该有可用的 git 完成

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

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

回答by Chao

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. More details about the plugin can be found here: https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/git

把上面那行注释掉,开启git插件,问题就解决了。关于插件的更多细节可以在这里找到:https: //github.com/robbyrussell/oh-my-zsh/tree/master/plugins/git