LoginSignup
3
3

More than 3 years have passed since last update.

シェルをbashからzshに変更する

Last updated at Posted at 2020-04-28

シェルをbashからzshに変更する

 1. zsh 導入
 2. sh-completion 補完拡張
 3. prezto コンフィグレーションフレームワーク
 4. peco コマンド履歴検索

zsh導入手順

1.zshインストール

$ brew install zsh

2.補完の拡張「zsh-completion」のインストール

zsh-completionをインストールする

$ brew install zsh-completion

zsh-completionを有効にする

$ vi .zshrc
if type brew &>/dev/null; then
 FPATH=$(brew --prefix)/share/zsh-completions:$FPATH
 autoload -Uz compinit
 compinit
fi

zcompdumpを再構築する
zcompdumpには、コマンドと関数が登録されている

$ rm -f ~/.zcompdump
$ compinit

[zsh compinit: insecure directories]が表示された場合

$ chmod go-w ‘/usr/local/share’

3.コンフィグレーションフレームワーク「prezto」のインストール

zshを起動する

$ zsh

preztoのリポジトリをクローンする

$ git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"

提供されている Zsh 設定ファイルをコピーして、新しい Zsh 設定を作成
.zshrcがあるとエラーになるため、.zshrcを別名にする

/Users/{user}/.zshrc: File exists

$ mv .zshrc .zshrc_temp

preztoが設定された.zshrcを作成する

$ setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
  ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done

コピーした.zshrcを追記する

$ cat .zshrc_temp >> .zshrc

4.コマンド履歴検索「peco」のインストール

pecoをインストールする

$ brew install peco

.zshrcに以下を追加する

$ vi .zshrc
peco-select-history() {
    BUFFER=$(history 1 | sort -k1,1nr | perl -ne 'BEGIN { my @lines = (); } s/^\s*\d+\*?\s*//; $in=$_; if (!(grep {$in eq $_} @lines)) { push(@lines, $in); print $in; }' | peco --query "$LBUFFER")
    CURSOR=${#BUFFER}
    zle reset-prompt
}
zle -N peco-select-history
bindkey '^r' peco-select-history

5.ログイン時のシェル変更

/etc/shellsの最後の行にパスを追加する

$ sudo vi /etc/shells
/usr/local/bin/zsh

ログイン時のシェルを変更する

$ chsh -s /usr/local/bin/zsh
Changing shell for <user>.
Password for <user>: 

細かい設定は、おいおい。。やっていきます!

6.ターミナルからzshを起動

スクリーンショット 2020-04-29 1.16.56.png

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3