zshでHomebrewを使用する場合に設定しておいたほうが良いこと
Homebrewの導入方法については以前の記事を参考にして下さい。
以下、homebrewおよびzshインストール後に設定していきます。
1.zsh流の環境変数PATHの設定方法
環境変数PAHTに "/usr/local/bin" などを追加する場合、zshでもbashと同じ方法で設定することができますが、今回はzsh流の設定方法を行いましょう。
以下の設定を "~/.zshrc" に追加して下さい。
## 重複パスを登録しない typeset -U path cdpath fpath manpath ## sudo用のpathを設定 typeset -xT SUDO_PATH sudo_path typeset -U sudo_path sudo_path=({/usr/local,/usr,}/sbin(N-/)) ## pathを設定 path=(~/bin(N-/) /usr/local/bin(N-/) ${path})
上記以外に設定するパスがある場合は適宜pathに追加して下さい。
解説
環境変数に "typeset -U" を設定することによって、登録済みのパスを追加しないようにしてくれます。
"source" コマンドなどで設定を再読み込みしたり、子シェルを起動した場合に何度も同じpathが登録されてしまう問題を解決してくれます。
path内の各パスの後ろについている "(N-/)" は、存在しないディレクトリは登録しないという設定です。
小文字のpathとは
小文字のpathはzshが管理している環境変数で、大文字のPATHとも紐付いています。
そのため、小文字/大文字のいずれか一方が変更された場合、自動的にもう一方にも変更が反映されるようになっています。
このような紐付けは、PATH/CDPATH/FPATH/MANPATH の4つがデフォルトで設定されています。
sudo_pathはユーザ独自の環境変数のため "typeset -xT" にて紐付けを行なっています。
分かりやすいように以下のコメントを設定ファイルに追加しておくと良いでしょう。
# typeset # -U 重複パスを登録しない # -x exportも同時に行う # -T 環境変数へ紐付け # # path=xxxx(N-/) # (N-/): 存在しないディレクトリは登録しない # パス(...): ...という条件にマッチするパスのみ残す # N: NULL_GLOBオプションを設定。 # globがマッチしなかったり存在しないパスを無視する # -: シンボリックリンク先のパスを評価 # /: ディレクトリのみ残す # .: 通常のファイルのみ残す
2.brewコマンドの補完設定
homebrewをインストールすると "/usr/local/Library/Contributions/" 配下にbashやzsh用のbrewコマンドの補完関数が追加されます。
$ ls -la /usr/local/Library/Contributions/ total 28K drwxr-xr-x 8 yonchu staff 272 4 7 19:19 ./ drwxr-xr-x 7 yonchu staff 238 4 7 19:13 ../ -rw-r--r-- 1 yonchu staff 11K 4 7 19:19 brew_bash_completion.sh -rw-r--r-- 1 yonchu staff 2.2K 4 7 19:19 brew_fish_completion.fish -rw-r--r-- 1 yonchu staff 2.9K 4 7 19:19 brew_zsh_completion.zsh drwxr-xr-x 18 yonchu staff 612 4 12 17:46 cmds/ -rw-r--r-- 1 yonchu staff 4.4K 4 7 19:19 install_homebrew.rb drwxr-xr-x 3 yonchu staff 102 4 7 19:19 manpages/
bashの場合
bashの場合は "~/.bashrc" 等で、"brew_bash_completion.sh" を読み込むよう設定するだけです。
# completionファイルの読み込み if type brew >/dev/null 2>&1; then BREW_PREFIX=$(brew --prefix) if [ -e $BREW_PREFIX/Library/Contributions/brew_bash_completion.sh ]; then source $BREW_PREFIX/Library/Contributions/brew_bash_completion.sh >/dev/null 2>&1 fi fi
zshの場合
zshの場合は以下のように設定します。
$ cd /usr/local/share/zsh/site-functions $ ln -s ../../../Library/Contributions/brew_zsh_completion.zsh _brew
(※ 補完関数のファイル名は "_コマンド名" にするという決まりになっています)
設定確認
$ ls -la /usr/local/share/zsh/site-functions total 8.0K drwxr-xr-x 5 yonchu staff 170 2012-10-09 15:41 ./ drwxr-xr-x 5 yonchu staff 170 2012-07-27 16:35 ../ lrwxr-xr-x 1 yonchu staff 54 4 19 02:41 _brew -> ../../../Library/Contributions/brew_zsh_completion.zsh
これでzshの再起動(または設定ファイルの再読み込み)を行えば、brewコマンドでも補完が行えます。
$ brew ここでタブ入力 brew subcommand: cat -- display formula file for a formula cleanup -- uninstall unused and old versions of packages create -- create a new formula deps -- list dependencies and dependants of a formula doctor -- audits your installation for common issues edit -- edit a formula home -- visit the homepage of a formula or the brew project info -- information about a formula install -- install a formula link -- link a formula list -- list files in a formula or not-installed formulae log -- git commit log for a formula missing -- check all installed formuale for missing dependencies. outdated -- list formulas for which a newer version is available prune -- remove dead links remove -- remove a formula search -- search for a formula (/regex/ or string) server -- start a local web app that lets you browse formulae (requires Sinatra) unlink -- unlink a formula update -- freshen up links upgrade -- upgrade outdated formulae uses -- show formulas which depend on a formula
補完関数についての補足
homebrewでインストールされたコマンドには、まだあまり多くはありませんがzsh用の補完関数が付属していることがあります。
補完関数はコマンドインストール時に "/usr/local/share/zsh/site-functions" 配下にシンボリックリンクが貼られます。
$ ls -la /usr/local/share/zsh/site-functions total 12K drwxr-xr-x 5 yonchu staff 170 10 9 15:41 ./ drwxr-xr-x 5 yonchu staff 170 7 27 16:35 ../ lrwxr-xr-x 1 yonchu staff 54 10 9 15:39 _ack -> ../../../Cellar/ack/1.96/share/zsh/site-functions/_ack lrwxr-xr-x 1 yonchu staff 54 4 19 02:41 _brew -> ../../../Library/Contributions/brew_zsh_completion.zsh lrwxr-xr-x 1 yonchu staff 60 4 7 2012 _git-now -> ../../../Cellar/git-now/0.1.0.9/share/zsh/functions/_git-now
3.自作の補完関数などを置くディレクトリの作成
ホームディレクトリにディレクトリを作成します。
$ mkdir -p ~/.zsh/functions/Completion $ ls -la ~/.zsh/functions total 0 drwxr-xr-x 3 yonchu staff 102 5 6 00:53 ./ drwxr-xr-x 4 yonchu staff 136 5 6 00:53 ../ drwxr-xr-x 4 yonchu staff 136 5 6 00:53 Completion/
次に "~/.zshrc" に作成したディレクトリの設定を追加します。
(環境変数 "fpath" に設定されているディレクトリが読み込まれます)
fpath=(~/.zsh/functions/Completion(N-/) ${fpath})
autoload -U compinit compinit -u
設定確認
$ echo $fpath | sed 's/ /\ /g' /Users/yonchu/.zsh/functions/Completion/ /usr/local/share/zsh/site-functions /usr/local/Cellar/zsh/5.0.0/share/zsh/functions
今後、補完関数を追加する場合にこのディレクトリに追加していきます。
補完関数を自作する場合もそうですが、既に他の方が補完関数を作っている場合もあるので、そういったものを探して追加していくと良いでしょう。
4.補完関数を追加
zsh-users/zsh-completionsという補完関数群を追加することで公式では対応していない様々なコマンドで補完が行えるようになります。
参考
インストール方法
以下のようにGithubよりclone
$ cd ~/.zsh/functions/Completion $ git clone https://github.com/zsh-users/zsh-completions.git
※ 自分の環境(dotfiles)をGitで管理している場合は、submoduleとして追加しましょう
つづいて、"~/.zshrc" に環境変数 "fpath" の設定を追加
# zsh-completions # https://github.com/zsh-users/zsh-completions.git fpath=(~/.zsh/functions/Completion/zsh-completions(N-/) ${fpath})
設定確認
$ echo $fpath | sed 's/ /\ /g' /Users/yonchu/.zsh/functions/Completion/ /Users/yonchu/.zsh/functions/Completion/zsh-completions/ /usr/local/share/zsh/site-functions /usr/local/Cellar/zsh/5.0.0/share/zsh/functions
以上です。
これでzshとhomebrewがさらに使いやすくなれば幸いです。