Git tab completion not working in zsh on mac

不问归期 提交于 2020-11-30 02:45:09

问题


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.

I've reinstalled oh-my-zsh but that didn't seem to help or make any difference.

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:

  • Switched to using zsh as my default shell
  • Installed oh-my-zsh
  • Downloaded https://github.com/git/git/blob/master/contrib/completion/git-completion.bash and saved it in ~/.completion/git/git-completion.sh
  • Downloaded https://github.com/git/git/blob/master/contrib/completion/git-completion.zsh and saved that in ~/.zsh/_git
  • Added zstyle ':completion:*:*:git:*' script ~/.completion/git/git-completion.sh

No luck.


回答1:


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)

Adding this to your .zshrc should be enough:

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)

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.

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

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. Hence, it has nothing to do with the topic of "Git completion in ZSH"




回答2:


The answer was that I had alias git=hub in my .zshrc file. See https://github.com/github/hub for info on hub (it's awesome).

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




回答3:


For the 2019 viewer:

If you use ZSH:

brew install hub

mkdir ~/.zsh and mkdir ~/.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.
(Mine was cp /usr/local/etc/bash_completion.d/hub.bash_completion.sh ~/.zsh/completions/_hub)

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

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

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

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




回答4:


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. Find out what versions your git install is and then remove the zsh autocompletions. 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.




回答5:


The same issue bothers me today. Inspired by the answers, I looked around the .zshrc and found this:

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



来源:https://stackoverflow.com/questions/24513873/git-tab-completion-not-working-in-zsh-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!