Mac中设置git、docker命令行自动补全

安装Homebrew

首先需要安装Homebrew,可在官网首页找到安装方法:

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

安装bash-completion

然后使用brew安装bash-completion:

brew install bash-completion

安装之后调用 brew info bash-completion 命令,根据提示将指令添加到 ~/.bash_profile 中:

➜  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 (189 files, 607.9KB) *
  Poured from bottle on 2020-05-04 at 01:32:28
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: 10,733 (30 days), 36,745 (90 days), 167,689 (365 days)
install-on-request: 9,927 (30 days), 33,958 (90 days), 154,120 (365 days)
build-error: 0 (30 days)

将如下加入 ~/.bash_profile 中

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

QQ20200504-123535.jpg

下载git补全脚本

添加git补全支持

# 进入目录
➜  ~ cd /usr/local/etc/bash_completion.d

# 下载脚本
➜  bash_completion.d curl -L -O http://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 73053  100 73053    0     0  68465      0  0:00:01  0:00:01 --:--:--  299k

# 软连接
➜  bash_completion.d brew unlink bash-completion
Unlinking /usr/local/Cellar/bash-completion/1.3_3... 182 symlinks removed
➜  bash_completion.d brew link bash-completion
Linking /usr/local/Cellar/bash-completion/1.3_3... 182 symlinks created

docker补全脚本

本地补全脚本

安装docker后,本地补全脚本可以在/Applications/Docker.app/Contents/Resources/etc/docker.bash-completion路径下找到。

QQ20200504-124521.jpg

➜  ~ cd /usr/local/etc/bash_completion.d

➜  bash_completion.d ln -s /Applications/Docker.app/Contents/Resources/etc/docker.bash-completion

➜  bash_completion.d ln -s /Applications/Docker.app/Contents/Resources/etc/docker-machine.bash-completion

➜  bash_completion.d ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.bash-completion

安装完成如下,重启终端即可


未经允许请勿转载:程序喵 » Mac中设置git、docker命令行自动补全

点  赞 (0) 打  赏
分享到: