一 、安装bash-completion
bash-completion 如果按照过了,请跳过步骤
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ brew install bash-completion <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">### 安装完成以后 查看</span> $ brew info bash-completion <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">### 会输出类似以下内容</span> ==> Caveats Add <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> following lines <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> your ~/.bash_profile: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -f $(brew <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--prefix)/etc/bash_completion ]; then</span> . $(brew <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--prefix)/etc/bash_completion</span> fi Homebrew's own bash completion <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">script</span> has been installed <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> /usr/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">local</span>/etc/bash_completion.d</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
将if…then…那一句添加到~/.bash_profile(如果没有该文件,新建一个)
重启终端,以上为安装bash-completion部分。
二、拷贝文件、设置路径
访问下面网站
https://github.com/git/git.git
找到”contrib/completion/”目录下的git-completion.bash ,然后点击编辑,拷贝其内容,复制到文本文件,保存为 git-completion.bash 文件
然后将文件用命令拷贝到 ~/ 目录下
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">cp</span> xxx/git-completion<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bash</span> ~/<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span>-completion<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bash</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
xxx 为文件所在目录,注意拷贝后的文件名称为 .git-completion.bash
在~/.bashrc文件(该目录下如果没有,新建一个)中添加下边的内容
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ source ~/<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span>-completion<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bash</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
三、 启动: 终端输入
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">$ source ~/<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.git</span>-completion<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bash</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
不输入的话,不会起作用
将下面这句话 添加到~/.bash_profile
<code class="hljs bash has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-operator" style="box-sizing: border-box;">-f</span> ~/.git-completion.bash ]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> . ~/.git-completion.bash <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
不添加想要补全起作用,每次都需要执行
source ~/.git-completion.bash
补全才能生效
原文:http://blog.youkuaiyun.com/chenbifeng/article/details/51570606