Mac git命令自动补全

在Mac上安装了git之后,发现命令不能自动补全,本文介绍怎么让git命令能够自动补全。

确保bash能够自动

在终端(本文使用的是OS X的终端)执行如下命令:

ccli@ccli-mac:dal$ brew list

看看是否已经安装有bash-completion,比如我的机器(已经安装了)运行上面的命令会显示:

bash-completion glib libtiff perl sqlite

如果没有安装,运行如下命令安装bash-completion:

brew install bash-completion

等待安装完成之后,运行如下命令:

brew info bash-completion

运行上面的命令后会在终端显示下图: 

bash-completion: stable 1.3 (bottled)

Programmable completion for Bash 3.2

https://salsa.debian.org/debian/bash-completion

Conflicts with:

  bash-completion@2 (because Differing version of same formula)

/usr/local/Cellar/bash-completion/1.3_3 (190 files, 676.4KB) *

  Poured from bottle on 2018-05-28 at 09:59:40

From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/bash-completion.rb

==> Caveats

Add the following line to your ~/.bash_profile:  [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

Bash completion has been installed to:  /usr/local/etc/bash_completion.d

==> Analytics

install: 14,875 (30 days), 44,147 (90 days), 189,622 (365 days)install_on_request: 13,491 (30 days), 40,092 (90 days), 173,591 (365 days)build_error: 0 (30 days)

仔细阅读箭头所指的地方,依照提示将这行内容

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

添加到 ~/.bash_profile文件(如果没有该文件,新建),然后重启终端,bash-completion功能安装完成。

让git支持自动补全

从github上clone git的源码到本地:

git clone https://github.com/git/git.git

找到”contrib/completion/”目录下的git-completion.bash,将该文件拷贝到~/目录下下并重命名为.git-completion.bash:

cp git-completion.bash ~/.git-completion.bash

在~/.profile文件中追加如下内容:

source ~/.git-completion.bash

重启终端,现在git能够使用tab键自动补全命令了,enjoy it!

推荐阅读更多精彩内容