Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2722

macのデフォルトシェルをbashからzshに乗り換える

$
0
0

MacをCatalinaに上げてからターミナルを起動するたびに下記の文章が出るようになったので重い腰を上げてbashからzshに乗り換えることにしました。

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.

'bash zsh mac'とかでググると似たような記事はたくさんありますが、この記事ではzshが入っている状態でbashからzshへの移行する手順を書いていきます。

手順

設定ファイルの準備

最初にbashの設定ファイルをzsh用にコピーします。
履歴が無くなったら作業効率がかなり悪くなるので忘れずコピーします。

cp -p .bash_profile .zprofile
cp -p .bashrc .zshrc
cp -p .bash_history .zsh_history

プロンプトを変更

- export PS1="\W:\$ "
+ export PROMPT="%{$fg[cyan]%}%c%{$reset_color%} %% "

プロンプトの文法が変わってるんですね。
元々カレントディレクトリだけ表示させていたので、そのままの仕様で移行しました。
変更ついでに色をつけてみたり、最後の記号はzshのデフォルトに合わせて%にしてみました。

この機会にもっとオシャレにならないかと思って世の中の人はどうしているのかググってみましたが2行プロンプトの記事が多くて自分の好みには合わなかったのでやめました。
↓が私の環境のキャプチャです(bash->zsh)。Simple is best!!
スクリーンショット 2019-11-07 17.21.33.png

tab保管

gitのtab保管やgcp-sdkの設定が壊れてしまったので修正。
zsh用の設定もすでにインストールされていたのでパスを変えるだけでした。

  • git
- if [ -f $(brew --prefix)/etc/bash_completion ]; then
-   . $(brew --prefix)/etc/bash_completion
- elif [ -f $(brew --prefix)/etc/bash_completion.d/git-completion.bash ]; then
-   . $(brew --prefix)/etc/bash_completion.d/git-completion.bash
- fi
+ fpath=($(brew --prefix)/share/zsh/site-functions $fpath)
+ autoload -U compinit
+ compinit -u
  • google cloud sdk
# The next line updates PATH for the Google Cloud SDK.
- if [ -f '/install/pass/google/google-cloud-sdk/path.bash.inc' ]; then . '/install/pass/google/google-cloud-sdk/path.bash.inc'; fi
+ if [ -f '/install/pass/google/google-cloud-sdk/path.zsh.inc' ]; then . '/install/pass/google/google-cloud-sdk/path.zsh.inc'; fi

# The next line enables shell command completion for gcloud.
- if [ -f '/install/pass/google/google-cloud-sdk/completion.bash.inc' ]; then . '/install/pass/google/google-cloud-sdk/completion.bash.inc'; fi
+ if [ -f '/install/pass/google/google-cloud-sdk/completion.zsh.inc' ]; then . '/install/pass/google/google-cloud-sdk/completion.zsh.inc'; fi

履歴検索

pecoとhistoryを使ってctrl+rの履歴検索をカスタマイズしていましたが動かなくなったので修正。
この対応で履歴検索がめちゃくちゃ高速なりました!頻繁に使うコマンドなのでもっと早くzshに移行すればよかった。。。

- peco-history() {
-     local NUM=$(history | wc -l)
-     local FIRST=$((-1*(NUM-1)))
-     if [ $FIRST -eq 0 ] ; then
-         history -d $((HISTCMD-1))
-         echo "No history" >&2
-         return
-     fi
-     local CMD=$(fc -l $FIRST | sort -k 2 -k 1nr | uniq -f 1 | sort -nr | sed -E 's/^[0-9]+[[:blank:]]+//' | peco | head -n 1)
-     if [ -n "$CMD" ] ; then
-         history -s $CMD
-         if type osascript > /dev/null 2>&1 ; then
-             (osascript -e 'tell application "System Events" to keystroke (ASCII character 30)' &)
-         fi
-     else
-         history -d $((HISTCMD-1))
-     fi
- bind -x '"\C-r":peco-history'
+ function peco-history-selection() {
+     BUFFER=`history -n 1 | tail -r  | awk '!a[$0]++' | peco`
+     CURSOR=$#BUFFER
+     zle reset-prompt
+ }
+ zle -N peco-history-selection
+ bindkey '^R' peco-history-selection

ペーストした時に色が反転する

zshにしてからターミナルに文字列を貼り付けると貼り付けた文字だけ色が反転するようになりました。bashだと同じ操作をしてもこのような挙動はしませんでした。
これは直し方がわからなかった。。。知っている方いたら教えてくださいmm
e.g. 'hogehoge'をペーストした場合、下記のようになります。
スクリーンショット 2019-11-07 18.05.37.png

その他

aliasや履歴検索以外のシンプルなfunctionは特に問題なく動きました。
そこまでゴリゴリにカスタマイズしていない人は結構すんなり移行できると思います。


Viewing all articles
Browse latest Browse all 2722

Trending Articles