Bash-Completion で SSH のホスト名を自動補完


## Bash-Completion とは

Bash の補完機能を拡張するシェルスクリプト

## bash-completion のインストール

### Mac

```homebrew
$ brew install bash-completion
```

```macports
$ sudo port install bash-completion
```


### RedHat

```redhat
$ yum install bash-completion
```

### Debian

```debian
$ apt-get install bash-completion
```

## MacPorts でインストールしてみた

```shell
$ sudo port install bash-completion
Portfile changed since last build; discarding previous state.
--->  Computing dependencies for bash-completion
--->  Dependencies to be installed: bash
--->  Fetching archive for bash
--->  Attempting to fetch bash-4.3.42_0.darwin_14.x86_64.tbz2 from http://jog.id.packages.macports.org/macports/packages/bash
--->  Attempting to fetch bash-4.3.42_0.darwin_14.x86_64.tbz2.rmd160 from http://jog.id.packages.macports.org/macports/packages/bash
--->  Installing bash @4.3.42_0
--->  Activating bash @4.3.42_0
--->  Cleaning bash
--->  Fetching archive for bash-completion
--->  Attempting to fetch bash-completion-2.1_10.darwin_14.noarch.tbz2 from http://jog.id.packages.macports.org/macports/packages/bash-completion
--->  Attempting to fetch bash-completion-2.1_10.darwin_14.noarch.tbz2 from http://packages.macports.org/bash-completion
--->  Attempting to fetch bash-completion-2.1_10.darwin_14.noarch.tbz2.rmd160 from http://packages.macports.org/bash-completion
--->  Installing bash-completion @2.1_10
--->  Activating bash-completion @2.1_10

To use bash_completion, add the following lines at the end of your .bash_profile:
  if [ -f /opt/local/etc/profile.d/bash_completion.sh ]; then
      . /opt/local/etc/profile.d/bash_completion.sh
  fi
The port bash-completion >=2.0 requires bash >=4.1; please make sure
you are using /opt/local/bin/bash by changing the preferences of your
terminal accordingly. If your version of bash is too old, the script
above will not modify your shell environment and no extended completion
will be available.


--->  Cleaning bash-completion
--->  Updating database of binaries
--->  Scanning binaries for linking errors
--->  No broken files found.
```

### bash_completion を使用するために .bash_profile に設定を追加

bash-completion インストール時、bash_completion を使用するには、 .bash_profile の最後に次の行を追加します。
と下記のような記載があるため、その通り行に追加する。

> To use bash_completion, add the following lines at the end of your .bash_profile:
>   if [ -f /opt/local/etc/profile.d/bash_completion.sh ]; then
>       . /opt/local/etc/profile.d/bash_completion.sh
>   fi

```shell
$ {
> echo "if [ -f /opt/local/etc/profile.d/bash_completion.sh ]; then"
> echo "    . /opt/local/etc/profile.d/bash_completion.sh"
> echo "fi"
> } >> ~/.bash_profile
```

## .bash_profile の読み込み

```shell
$ source ~/.bash_profile
```

## 補完一覧が表示されるかの確認

```shell
$ ssh <TAB>
```

と、実行したがうまく補完が効かない…。  
他の方々の記事を見るとほとんどこの時点でうまくいっている様子だ…。


MacPorts の PATH辺りが怪しいと思ったので、[howto/bash-completion – MacPorts](https://trac.macports.org/wiki/howto/bash-completion#Howtousebash-completion "howto/bash-completion – MacPorts")を見てみたところ、下記のように記載されていた。  
ターミナルの設定が必要みたい。

> Step 3: Use bash from MacPorts in your terminal
> 
> The port bash-completion at version 2.0 requires at least bash at version 4.1; the older bash 3.2 provided by Apple with Mac OS > X is not compatible anymore with this version. Please make sure you are using /opt/local/bin/bash by changing the preferences > of your terminal accordingly. If your version of bash is too old, the script above will not modify your shell environment and > no extended completion will be available.
> 
> Terminal.app
> 
> You need to change the command Terminal.app uses to launch the shell in the preferences.
> 
> Menu > Preferences > Startup, "Shells open with:"
> Select "Command" and enter /opt/local/bin/bash -l to switch to bash provided by MacPorts.
> Menu > Preferences > Settings > Shell (per shell type), "Prompt before closing:"
> Click "+" and add "bash" to the list of processes.
> Close and reopen any terminal windows
> iTerm2
> 
> You need to change the command iTerm2 uses to launch the shell in the appropriate profile in the preferences.
> 
> Menu > Preferences > Profiles tab
> Select your profile, on the right switch to the General tab, see "Command"
> Select "Command:" and enter /opt/local/bin/bash -l
> Close and reopen any terminal windows
> Add /opt/local/bin/bash to the file /etc/shells, otherwise it will be impossible to use AppleScript to tell Terminal to execute > scripts (like 'osascript -e "tell application \"Terminal\" to do script \"echo hello\""').> 

## ターミナルの設定

[howto/bash-completion – MacPorts](https://trac.macports.org/wiki/howto/bash-completion#Howtousebash-completion "howto/bash-completion – MacPorts")の記載通り、お使いの環境に合わせ **Terminal.app** 又は、 **iTerm2** を設定し、ターミナルを再起動する。

## tmuxの設定

tmuxを使ってる方は、[howto/bash-completion – MacPorts](https://trac.macports.org/wiki/howto/bash-completion#Howtousebash-completion "howto/bash-completion – MacPorts")の記載通り .tmux.conf の行に追加する。

> tmux
>
> For tmux, you need to set set -g default-shell "/opt/local/bin/bash" in your .tmux.conf.

```shell
$ echo 'set -g default-shell "/opt/local/bin/bash"' >> ~/.tmux.conf
```


## 再度補完一覧が表示されるかの確認

```shell
$ ssh <TAB>
```

出来た！



## 参考サイト

* [Bash-Completion](http://bash-completion.alioth.debian.org/ "")
* [howto/bash-completion – MacPorts](https://trac.macports.org/wiki/howto/bash-completion#Howtousebash-completion "Bash-Completion")
* [Bash - sshのhost名を自動補完させる - Qiita](http://qiita.com/soramugi/items/846e6eac0ce1d3dc1e42 "Bash - sshのhost名を自動補完させる - Qiita")