Homebrew 的`git` 不使用补全

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

Homebrew’s `git` not using completion

macosgitautocompletezsh

提问by user137369

When using OSX's git, after I modify a file I can simply do git commit <tab>, and that'll auto complete the file's name to the one that was modified. However, if I install a newer version of git from homebrew and I use it, that feature no longer works (meaning I press <tab>and it just “asks” me what file I want to do it on, even including the ones that have no changes).

使用 OSX 的 git 时,在我修改文件后,我可以简单地执行git commit <tab>,然后将文件名自动补全为修改后的文件名。但是,如果我从自制软件安装更新版本的 git 并使用它,该功能将不再有效(这意味着我按下<tab>它,它只会“询问”我想要在哪个文件上执行此操作,甚至包括那些没有更改的文件) )。

Can anyone shed some light as to why, and how to solve that? I'd prefer using homebrew's git, since it's more up-to-date.

任何人都可以解释为什么,以及如何解决这个问题?我更喜欢使用 homebrew 的 git,因为它是最新的。

My shell is zsh, and Neither installing bash-completionor zsh-completionsworked (even after following homebrew's post-install instructions).

我的壳是zsh的,既不安装bash-completionzsh-completions工作(甚至在以下自制的后安装说明)。

Also, after installing git with homebrew it says

此外,在用自制软件安装 git 后,它说

Bash completion has been installed to: /usr/local/etc/bash_completion.d
zsh completion has been installed to: /usr/local/share/zsh/site-functions

So shouldn't I be able to use one of those?

那么我不应该能够使用其中之一吗?

回答by Graham Perks

You're looking for:

您正在寻找:

brew install git bash-completion

As warpc's comment states, you'll need to add the following to your ~/.bash_profileto get homebrew's bash-completion working:

正如 warpc 的评论所述,您需要将以下内容添加到您的内容中,~/.bash_profile以使自制软件的 bash 完成工作:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi

The above is mentioned in the caveats when you install the bash-completion formula.

安装 bash-completion 公式时的注意事项中提到了上述内容。



Note:if you are using Bash v4 or later (via brew install bash) then you're going to want to use brew install bash-completion@2, to enable tab completion add the following to ~/.bash_profileas described in the caveats:

注意:如果您使用的是 Bash v4 或更高版本(通过brew install bash),那么您将要使用brew install bash-completion@2, 以启用选项卡完成将以下内容添加到~/.bash_profile警告中所述:

export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

The additional export is necessary for git, docker, youtube-dl, and other completions which may be included in the $(brew --prefix)/etc/bash_completion.d/directory.

附加导出对于 git、docker、youtube-dl 和其他可能包含在$(brew --prefix)/etc/bash_completion.d/目录中的完成是必要的。

回答by user2347638

This get's git tab completion working on OSX without having to restart your terminal:

这个 get 的 git tab 完成在 OSX 上工作而无需重新启动终端:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash && echo "source ~/.git-completion.bash" >> ~/.bash_profile && source ~/.bash_profile

回答by Harvey

In case anyone else makes my dumb mistake, try brew install git. I was using the gitthat comes with Xcode and didn't realize that I had never installed Homebrew's git to get the autocompletions.

如果其他人犯了我的愚蠢错误,请尝试brew install git。我正在使用gitXcode 附带的,并没有意识到我从未安装过 Homebrew 的 git 来获取自动完成功能。

回答by Anentropic

for some reason I was missing the file at $(brew --prefix)/etc/bash_completionso @Graham Perks' correct answer didn't work for me

出于某种原因,我错过了该文件,$(brew --prefix)/etc/bash_completion所以@Graham Perks 的正确答案对我不起作用

It ended up the fix in my case was:

在我的情况下,它最终的修复是:

brew unlink bash-completion
brew link bash-completion

回答by Asbj?rn Ulsberg

I solved the problem by figuring out that $(brew --prefix)/etc/bash_completionreturned Permission deniedwhen executed. So after a simple:

我通过找出执行时$(brew --prefix)/etc/bash_completion返回的来解决了这个问题Permission denied。所以经过一个简单的:

chmod +x $(brew --prefix)/etc/bash_completion

Everything is now working fine. I'm wondering why Homebrew doesn't make the bash_completionfile executable on installation, though.

现在一切正常。不过,我想知道为什么 Homebrew 不会bash_completion在安装时使文件可执行。

回答by Jonathan Knapp

I had the same issue and even found this post this morning. I fixed the issue by updating brew with brew updateand then reinstalling git with brew reinstall git.

我有同样的问题,甚至今天早上发现了这篇文章。我通过更新 brew withbrew update然后重新安装 git解决了这个问题brew reinstall git

I was then notified of another file that is blocking the homebrew linking process, in my case it was /usr/local/share/zsh/site-functions/git-completion.bash. Removing the file and running brew link gitsolved the issue. Guessing it was just a bad recipe version we stumbled upon.

然后我被通知另一个阻止自制程序链接过程的文件,在我的情况下是/usr/local/share/zsh/site-functions/git-completion.bash. 删除文件并运行brew link git解决了问题。猜测这只是我们偶然发现的一个糟糕的食谱版本。

回答by user137369

Found a working solution. It's very recent (authored 16 hours ago, and committed 2 hours ago), and it comes directly from homebrew.

找到了一个有效的解决方案。它是最近的(16 小时前创作,2 小时前提交),它直接来自 homebrew

brew install git --without-completions

brew install git --without-completions

Just tried it, and it finally works as intended.

刚刚试了一下,终于按预期工作了。

回答by dlamblin

If you have $BASH_VERSION< 4.1, eg 3.2.57(1)-releasethen go ahead with:

如果你有$BASH_VERSION< 4.1,例如3.2.57(1)-release然后继续:

brew install bash-completion
# In ~/.bash_profile :
if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi

However if you've brew install bashto get version 4.4.12(1)-releaseyou can use the better and more complete completions in:

但是,如果您必须brew install bash获得版本4.4.12(1)-release,则可以在以下位置使用更好更完整的完成:

brew install bash-completion@2
# In ~/.bash_profile:
[ -f "$(brew --prefix)/share/bash-completion/bash_completion" ] \
&& . "$(brew --prefix)/share/bash-completion/bash_completion"

Note that some packages (brew, docker, tmux) will still put some completions into $(brew --prefix)/etc/bash_completion.d/so you might add:

请注意,某些软件包(brew、docker、tmux)仍会$(brew --prefix)/etc/bash_completion.d/添加一些补全,因此您可以添加:

for completion in "$(brew --prefix)/etc/bash_completion.d/"*
do
    . $completion
done

Finally you should be able to add the git completion script if for some reason the way you installed git did not add it to either of those:

最后,如果由于某种原因,您安装 git 的方式没有将其添加到其中任何一个中,您应该能够添加 git 完成脚本:

[[ -f $(brew --prefix)/etc/bash_completion.d/git \
|| -f $(brew --prefix)/share/bash-completion/completions/git ]] \
|| curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash \
    -o $(brew --prefix)/etc/bash_completion.d/git

You can get and add it with the above.

您可以使用上述内容获取并添加它。

回答by Feuda

Step 1: Download auto completion script:

第 1 步:下载自动完成脚本:

cd ~
curl -O https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

Step 2: Update .bash_profile and .bashrc

第 2 步:更新 .bash_profile 和 .bashrc

echo "source ~/git-completion.bash" >> .bash_profile

Via https://www.anintegratedworld.com/git-tab-autocomplete-on-osx-10-11-el-capitan/

通过https://www.anintegratedworld.com/git-tab-autocomplete-on-osx-10-11-el-capitan/

If above does not work, try https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion

如果以上不起作用,请尝试https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion

回答by ar31an

This worked for me in Mojave (OSX 10.14.1):

这在 Mojave (OSX 10.14.1) 中对我有用:

brew install bash-completion

Then add the following line to your ~/.bash_profile:

然后将以下行添加到您的 ~/.bash_profile 中:

[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion