Mac下Git命令自动补齐

Mac下Git命令自动补齐

Mac系统下默认不支持Git的自动补齐功能,需要自己进行配置才能实现该功能。这里借助HomeBrew下的bash-completion实现。

安装HomeBrew

1
sudo /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装bash-completion

1
brew install bash-completion

安装完成后使用如下命令查看

1
brew info bash-completion

输出结果如下

1
2
3
4
5
6
7
8
==> Caveats
Add the following lines to your ~/.bash_profile:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi

Homebrew's own bash completion script has been installed to
/usr/local/etc/bash_completion.d

根据提示将[if…fi]部分加入到~/.bash_profile中。之后重启终端。

git-completion.bash设置

新建~/.git-completion.bash,访问https://github.com/git/git.git, 查看contrib/completion/git-completion.bash,将该文件内的所有内容复制到~/.git-completion.bash下 。

添加如下命令到~/.bash_profile

1
2
3
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi

重新打开终端或者输入source ~/.bash_profile进行激活。