特にこだわりはないのですが、bashを使い続けていた自分に疑問を感じ、zshに冒険してみることにしました。幸いにもhomebrewで導入できるようですし、設定ファイルも共存できるようなので(もとの.bash_profileを書き換える必要もない)、試してみます。zshを使いながら自分のスキルの向上を望むばかりです(笑
zsh
zshはbashの拡張版らしく、コマンドの補完機能等がbashよりも発達しているようです。macではターミナルの画面分割なども可能になるようです。(このあたりは大雑把で)
導入
zshはhomebrewで導入します。
brew install zsh
ではzsh本体を、
brew install zsh-completions
ではzshのコード補完機能を導入できます。私の環境では
/usr/local/Cellar/zsh
以下にインストールされました。この際、後者のインストールにおいて
To activate these completions, add the following to your .zshrc:
fpath=(/usr/local/share/zsh-completions $fpath)
You may also need to force rebuild `zcompdump`:
rm -f ~/.zcompdump; compinit
Additionally, if you receive "zsh compinit: insecure directories" warnings when attempting
to load these completions, you may need to run this:
chmod go-w '/usr/local/share'
と注意されるので、言われたとおりに実行しておきます。
この次にすべきことは、zshをシェルとして登録し、デフォルトにすることですね。それには
sudo vi /etc/shells
から、以下のように最終行(11行目)に追加します。このファイルはrootじゃないと書き込み保存できないので、sudoで編集する必要があります。
1 # List of acceptable shells for chpass(1).
2 # Ftpd will not allow users to connect who are not using
3 # one of these shells.
4
5 /bin/bash
6 /bin/csh
7 /bin/ksh
8 /bin/sh
9 /bin/tcsh
10 /bin/zsh
11 /usr/local/bin/zsh
このあとで
chsh -s /usr/local/bin/zsh
を実行してやれば、デフォルトにzshが設定され、bashとはおさらばです...
[コメント] インストール先とzshのディレクトリが異なるのは, エイリアスが/use/local/bin/zsh
に作られるためです。
権限エラーの対処
このままだと、ターミナルを立ち上げると
Ignore insecure directories and continue [y] or abort compinit [n]? y
と注意されてしまいます。zshの管理フォルダがinsecureになっているためですね。そのためにcompaudit
を実行して、insecureなディレクトリを探します。私の環境では
/usr/local/share/zsh/site-functions
/usr/local/share/zsh
がinsecureだったので、
chmod 755 /usr/local/share/zsh/site-functions
chmod 755 /usr/local/share/zsh
を実行して権限を変更しました。
.zprofile と .zshrc の設定
次に、.zprofileと .zshrcの設定をします。詳しくは記事の最後に挙げた参考記事を参照ください。
.zprofileについてはシンプルに
cat .bash_profile >> .zprofile
とそれまで使っていた内容を丸写ししました。
.zshrcについては、少し凝った.zshrcを使わせていただきました。導入したばかりなので、詳しい内容はまたの機会ということで。。。
その他メモ
Homebrew
とりえあえずちゃんと動いているか確認しました: brew doctor
. そしたらパスが通っていなかったようで
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc
と実行しました。(もともとの.bash_profileに追加していなかっただけかもしれませんが)
ROOT
ここで挙げているROOTというのは管理者権限のrootのことではなく、CERN(欧州原子核研究機構)で主に開発が進められているデータ処理ソフトのことです。起動のたびに
export BREWSYS=/usr/local/Cellar
source $BREWSYS/root/5.34.36_2/libexec/thisroot.sh
[2017/04/07現在のバージョン]
を実行しないといけないので、.bash_profileに登録していたのですが、これがまた
ERROR: must cd where/root/is before calling . libexec/thisroot.sh for this version of bash!
とエラーを吐いてしまいました。調べたところ
31 if [ "x${BASH_ARGV[0]}" = "x" ]; then
32 if [ ! -f libexec/thisroot.sh ]; then
33 ROOTSYS="$PWD"; export ROOTSYS
34 elif [ -f ./thisroot.sh ]; then
35 ROOTSYS=$(cd ..; pwd); export ROOTSYS
36 else
37 echo ERROR: must "cd where/root/is" before calling ". libexec/thisroot.sh" for this version of bash!
38 ROOTSYS=; export ROOTSYS
39 return 1
40 fi
41 ROOTSYS="$PWD"; export ROOTSYS
42 else
43 # get param to "."
44 thisroot=$(dirname ${BASH_ARGV[0]})
45 ROOTSYS=$(cd ${thisroot}/.. > /dev/null;pwd); export ROOTSYS
46 fi
と編集すれば良いようです。こうすればzshでもちゃんと動くようになります。
さぁzshを使いこなせるようにがんばろう!(笑
参考にした記事
以下の記事を参考にさせていただきました。ありがとうございました。
とりあえずZshを使えば良いんだろう?
[ROOT-7706] thisroot.sh not working properly for zsh
macでzshでzsh compinit: insecure directoriesの警告が出る問題