Git 完成在 OS X Yosemite 和 Homebrew 上的 zsh 中不起作用

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

Git completion not working in zsh on OS X Yosemite with Homebrew

githomebrewzsh

提问by Thomas Watson

I cannot get git completion to work on my freshly installed OS X Yosemite in the zsh shell. I've installed both git and zsh using homebrew:

我无法在 zsh shell 中让 git 完成在我新安装的 OS X Yosemite 上工作。我已经使用自制软件安装了 git 和 zsh:

brew install zsh git

When installing git via homebrew, it should setup tab-completion for you, but when I git <tab>, it just tries to tab-complete the directories, and not the git-commands. I.e. it's not giving any errors - it's just not working - like it's not set up.

通过自制软件安装 git 时,它应该为您设置制表符补全,但是当 I 时git <tab>,它只是尝试对目录进行制表符补全,而不是 git 命令。即它没有给出任何错误 - 它只是不起作用 - 就像它没有设置一样。

This is what I know so far

这是我目前所知道的

Apparently zsh will look for ways to autocomplete by looking for _*files in one of the directories in $fpath. This is mine:

显然 zsh 将通过_*$fpath. 这是我的:

% echo $fpath
/usr/local/share/zsh/site-functions /usr/local/Cellar/zsh/5.0.6/share/zsh/functions

Looking at the first path we see:

查看我们看到的第一条路径:

% ls -l /usr/local/share/zsh/site-functions
lrwxr-xr-x  1 watson  admin   55 Oct 20 12:08 _git -> ../../../Cellar/git/2.1.2/share/zsh/site-functions/_git
lrwxr-xr-x  1 watson  admin   70 Oct 20 12:08 git-completion.bash -> ../../../Cellar/git/2.1.2/share/zsh/site-functions/git-completion.bash

So it seems to be setup to work out of the box - but it doesn't.

所以它似乎被设置为开箱即用 - 但事实并非如此。

This is what I've also tried

这也是我尝试过的

git-completion.bash

git-completion.bash

  1. Downloading git-completion.bash
  2. Running it using source git-completion.bash
  1. 下载git-completion.bash
  2. 运行它使用 source git-completion.bash

This one kind of works (i.e. it enables auto-completion), but it prints a warning:

这种工作(即它启用自动完成),但它打印警告:

WARNING: this script is deprecated, please see git-completion.zsh

git-completion.zsh

git-completion.zsh

So because of the above warning, I obviously also tried to download git-completion.zshand followed the guide in the top of the file, but it basically tells you to source the .bash file first, which is of cause still giving an error.

因此,由于上述警告,我显然也尝试下载git-completion.zsh并按照文件顶部的指南进行操作,但它基本上告诉您首先获取 .bash 文件,这是导致仍然出现错误的原因.

Search StackOverflow and Google

搜索 StackOverflow 和 Google

I've tried just about everything I could find by searching here and on Google, but nothing seems to work for me.

我已经尝试了通过在此处和 Google 上搜索可以找到的几乎所有内容,但似乎没有任何效果对我有用。

回答by Thomas Watson

I just stumbled upon the answer!

我只是偶然发现了答案!

In my case I was missing a few important pieces in my .zshrcfile. But first a little background:

就我而言,我在我的.zshrc文件中遗漏了一些重要的部分。但首先是一点背景:

What I'm trying to do is setup the "zsh Completion System". It comes with a lot of commands all named something with comp*. I tried to run these a few times but in many cases zsh would just tell me it didn't know them. So aparently you have to autoloadthem, among other things.

我想要做的是设置“ zsh 完成系统”。它带有许多命令,所有命令都带有comp*. 我试图运行这些几次,但在很多情况下 zsh 只会告诉我它不知道它们。所以aparently你必须对autoload他们,除其他外。

This is what I did:

这就是我所做的:

I added the following lines to my .zshrcfile:

我在我的.zshrc文件中添加了以下几行:

autoload -U compinit && compinit
zmodload -i zsh/complist

Then I opened a new terminal and ran:

然后我打开一个新终端并运行:

rm -f ~/.zcompdump; compinit

Then I opened a new terminal and now git <tab>worked as expected :)

然后我打开了一个新终端,现在git <tab>按预期工作:)

If you are setting up a custom $fpathin your .zshrcfile I would recommend adding these lines afteryou've modified the $fpath(though I don't know if it makes a difference).

如果您要$fpath.zshrc文件中设置自定义,我建议您在修改添加这些行$fpath(尽管我不知道它是否有所不同)。

回答by neaumusic

My problem was much simpler; I forgot to change my default shell.

我的问题要简单得多;我忘了更改我的默认外壳。

echo $SHELLand if it's /bin/bashthen just type chsh -s /bin/zsh

echo $SHELL如果是,/bin/bash那么只需输入chsh -s /bin/zsh

回答by Matthieu Napoli

I removed the .zcompdump-(...)file I had in my home directory and plugins started working again.

我删除了.zcompdump-(...)主目录中的文件,插件再次开始工作。

回答by Jpsy

You have to do...

你必须要做...

brew install zsh-completion

Then add this block to your ~/.zshrc file (which you may have to create first):

然后将此块添加到您的 ~/.zshrc 文件(您可能必须先创建):

if type brew &>/dev/null; then
  FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

  autoload -Uz compinit
  compinit
fi

See my answer on AskDifferent for more details:
https://apple.stackexchange.com/a/392382/77452

有关更多详细信息,请参阅我在 AskDifferent 上的回答:https://apple.stackexchange.com/a/392382/77452