天天看点

Mac下如何设置git completion

不得不说还说google大法好。不知道其他人有没有遇到过这个问题,就是在mac下想用git completion,意思就是在命令行敲git [tab]之后能显示git的智能提示指令,而不是显示当前文件夹下的文件,捣鼓了半天,终于搞定了。

首先我以为git-completion只有bash,而Mac下默认的zsh,所以是用不了这个脚本的,且我用brew下载git之后并没有像网上说的在

/usr/local/etc

下有一个文件夹名字为

gitgit-completion.d

,我的是在

/opt/homebrew/Cellar/git/2.31.1/etc/bash_completion.d

,但问题是这个目录下面只有

git-completion.bash

,并不是我想要的.zsh文件,经过一番搜索,发现在

/opt/homebrew/Cellar/git/2.31.1/share/zsh/site-functions

下有一个文件

_git

,打开查看第一行写着

# zsh completion wrapper for git

,顿时茅塞顿开。

剩下的事情就变得异常的简单,只能说git做得实在是太好了,因为接下来我甚至都不用查什么资料,该文件开头注释清清楚楚告诉我要怎么做了

# zsh completion wrapper for git
#
# Copyright (c) 2012-2020 Felipe Contreras <[email protected]>
#
# The recommended way to install this script is to make a copy of it as a
# file named '_git' inside any directory in your fpath.
#
# For example, create a directory '~/.zsh/', copy this file to '~/.zsh/_git',
# and then add the following to your ~/.zshrc file:
#
#  fpath=(~/.zsh $fpath)
#
# You need git's bash completion script installed. By default bash-completion's
# location will be used (e.g. pkg-config --variable=completionsdir bash-completion).
#
# If your bash completion script is somewhere else, you can specify the
# location in your ~/.zshrc:
#
#  zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
#
           

不过我没按这个做法,稍微改了一下,将上述最后一行

script ~/.git-completion.bash

修改为

script /opt/homebrew/Cellar/git/2.31.1/share/zsh/site-functions/_git

,再手动source一下,就ok了。

参考文档:

https://oliverspryn.medium.com/adding-git-completion-to-zsh-60f3b0e7ffbc

继续阅读