4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Catalinaが「zshがデフォルトだよ!」っていうので、bashからzshに変えてみたらすごい良かった

Posted at

環境

Mac OS 10.15.7 Catalina
ターミナル (普段はiterm2)

背景

ターミナル開くたびに

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.

って言われてたのでzsh使うことにした。

zsh使ってみたら候補だしてくれるのがとても良かった。

本記事について

zshの設定記事はたくさんある。
本記事では、設定ファイルをどういじるとどう変わるのかを画像で詳しく説明する。

デフォルトのシェルをzshに変える

zshが入ってるかどうかを確認

$ /bin/zsh --version
zsh 5.7.1 (x86_64-apple-darwin19.0)

入ってた。
入ってなかったり、バージョン上げたい場合はzshのインストールから始める。(本記事ではスキップ)

そしたら、デフォルトのシェルをzshに切り替える

$chsh -s /bin/zsh

チェンジシェル。

そしてターミナルを新しく開く(Command + Nが早い )とだいたい以下のようになる。

スクリーンショット_2020-10-23_22_55_41.png

この状態から設定を変えていく。

設定ファイル .zshrcを書いていく

ターミナルで開いた状態で以下のコマンドで作る。

$touch .zshrc

編集はテキストエディタで行う。

まずはユーザー名等の部分を変更する。

.zshrc
# show git branch
# https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
source ~/.git-prompt.sh

# PROMPTの中の変数を展開するためのオプション
setopt PROMPT_SUBST

# -----------------------------
# Prompt
# -----------------------------
# %c    カレントディレクトリ(相対パス)
# %F{色指定}変えたい部分%f
PROMPT='%F{yellow}%c%f%F{green}$(__git_ps1 "(%s)")%f$ '

.git-prompt.shは上記のURLから事前にダウンロードして配置しておくこと。

すると、こんな感じで色付けされる。ブランチ名も表示される。

スクリーンショット 2020-10-23 23.05.46.png

次にもともと.bash_profile等で設定してあったPATH等を移動

.zshrc
...

# Path
export PATH=もともとあったパス

...
$ echo $PATH

でPATHが通ってるか確認。

ここからzshのカスタマイズ

zsh-completions

(https://formulae.brew.sh/formula/zsh-completions)

補完良い感じにしてくれる

スクリーンショット 2020-10-23 23.33.00.png

git cまで打ってからtabキー押すと、候補出してくれる。
そのままtabキー連打して選択できる。

$brew install zsh-completions
.zshrc
...

# zsh-completions
if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

    autoload -Uz compinit
    compinit
fi

...

zsh-syntax-highlighting

(https://formulae.brew.sh/formula/zsh-syntax-highlighting)

コマンドがあってると緑、間違ってると赤に色付く

スクリーンショット 2020-10-23 23.36.36.png スクリーンショット 2020-10-23 23.36.45.png
$brew install zsh-syntax-highlighting
.zshrc
...

# zsh-syntax-highlighting
source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

...

zsh-autosuggestions

(https://formulae.brew.sh/formula/zsh-autosuggestions)

コマンド入力する段階で履歴から予測してくれる

スクリーンショット 2020-10-23 23.41.02.png

右キーで決定できる

$brew install zsh-autosuggestions
.zshrc
...

# zsh-autosuggestions
source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh

...

tabキーでの補完候補の色を変える

ファイル名を指定するときとかにtabキーを押すと候補を出してくれる。
スクリーンショット 2020-10-23 23.43.53.png

tabキーを連打すると選択を変更できる。
スクリーンショット 2020-10-23 23.44.59.png

この候補にまず色をつける

スクリーンショット 2020-10-23 23.46.31.png

.zshrc
...

# 補完候補一覧をカラー表示 '=対象=色' 35はPink
zstyle ':completion:*'  list-colors '=*=35'

...

色はこちらを参照
(http://yonchu.hatenablog.com/entry/2012/10/20/044603)

そして、選択中の候補を明確にするようにする

スクリーンショット 2020-10-23 23.49.25.png

.zshrc
...

# 選択中の候補に背景色付け
zstyle ':completion:*:default' menu select=2

...

まとめ

最終的に.zshrcは以下のようになった。

.zshrc
# show git branch
# https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
source ~/.git-prompt.sh

# PROMPTの中の変数を展開するためのオプション
setopt PROMPT_SUBST

# -----------------------------
# Prompt
# -----------------------------
# %c    カレントディレクトリ(相対パス)
# %F{色指定}変えたい部分%f
PROMPT='%F{yellow}%c%f%F{green}$(__git_ps1 "(%s)")%f$ '

# Path
# ここはそれぞれ

# zsh-completions
if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

    autoload -Uz compinit
    compinit
fi

# zsh-syntax-highlighting
source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# zsh-autosuggestions
source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh

# 補完候補一覧をカラー表示 '=対象=色' 35はPink
zstyle ':completion:*'  list-colors '=*=35'
# 選択中の候補に背景色付け
zstyle ':completion:*:default' menu select=2

参考

https://blog.katsubemakito.net/macos/setup-macbookair2020
http://voidy21.hatenablog.jp/entry/20090902/1251918174

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?