Zsh インストールから初期設定まで

Linux
この記事は約12分で読めます。

※当サイトはアフィリエイトからの広告収入で運営されており、広告が表示される場合があります。

Zsh(Z shell)はPOSIXを採用しているシステムの多くが標準搭載しているBash(Bourne-again shell)と比べ、非常に拡張性の高いコマンドシェルだ。2019年にmacOSに採用されたことで急激にシェアを拡大した。

Zshには優れたオートコンプリート機能だけでなく、Oh My ZshやPretzoといったプラグインや見た目を変えるテーマの管理が簡単に行えるマネージャがある。何百種類ものプラグインやテーマが存在するため、自分のタスクや好みに合ったインターフェイスに手軽に変更することが可能。

両者ともBourne shellから派生したシェルで使い勝手が似通っているため、切り替えはスムーズなはずだ。

インストール

まずシステムに zsh がインストールされているか確認する。

$ which zsh

すでにインストールされていれば /usr/bin/zsh といった感じのメッセージが表示される。何も表示されなければインストールされていない。インストールされていなければ以下のコマンドを実行する。

Redhat系

$ yum install zsh

Debian系

$ sudo apt-get install zsh

Arch系

$ sudo pacman -S zsh

macOS系

※予め Homebrew をインストールしておく必要がある。
※Catalina以降のmacOS XはZshを標準採用しているのでインストールしなくても良い。

brew install zsh

コマンドシェルを変更する

Zshがインストールされたパスを調べる

$ which zsh
/usr/bin/zsh

以下のコマンドでシェルを変更する。

$ chsh

管理者パスワードと先程調べたZshのパスを入力する。

nemo のシェルを変更します。
パスワード:
新しいシェル [/bin/bash]: /usr/bin/zsh
シェルを変更しました。

X11を再起動したらZshがシステムの標準シェルとなっているはずだ。

初期設定ウィザード

zshを初めて立ち上げると zsh-newuser-install というスクリプトが走り、初期設定ウィザードが起動する。このウィザードで自動補完機能やコマンド履歴を簡単に設定することができる。

$ zsh
This is the Z Shell configuration function for new users, zsh-newuser-install.
You are seeing this message because you have no zsh startup files (the files .zshenv, .zprofile, .zshrc, .zlogin in the directory ~).  This function can help you with a few settings that should make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

--- Type one of the keys in parentheses --- 
  • q で何もせずに終了。
  • 0 で空の .zshrc ファイルを生成する。
  • 1 で設定メニューを表示する。
Please pick one of the following options:

(1)  Configure settings for history, i.e. command lines remembered
     and saved by the shell.  (Recommended.)

(2)  Configure the new completion system.  (Recommended.)

(3)  Configure how keys behave when editing command lines.  (Recommended.)

(4)  Pick some of the more common shell options.  These are simple "on"
     or "off" switches controlling the shell's features.  

(0)  Exit, creating a blank ~/.zshrc file.

(a)  Abort all settings and start from scratch.  Note this will overwrite
     any settings from zsh-newuser-install already in the startup file.
     It will not alter any of your other settings, however.

(q)  Quit and do nothing else.  The function will be run again next time.
--- Type one of the keys in parentheses --- 
  • 1 でコマンド履歴の設定。
  • 2 でオートコンプリートの設定。
  • 3 でショートカットキーについての設定。
  • 4 その他の設定。 
  • 0 で空の設定ファイルを出力。
  • a で設定をリセットしてスクリプトを再実行。
  • q 何もせずに終了。

コマンド履歴の設定

History configuration
=====================

# (1) Number of lines of history kept within the shell.
HISTSIZE=1000                                                             (not yet saved)
# (2) File where history is saved.
HISTFILE=~/.histfile                                                      (not yet saved)
# (3) Number of lines of history to save to $HISTFILE.
SAVEHIST=1000                                                             (not yet saved)

# (0)  Remember edits and return to main menu (does not save file yet)
# (q)  Abandon edits and return to main menu

--- Type one of the keys in parentheses --- 
  • 1 入力したコマンドの履歴をどれくらいシェルに保管するか設定する。 HISTSIZE=任意の数値 
  • 2 ログを保存する場所を設定する。 HISTFILE=任意のファイル名 
  • 3 HISTFILEにいくつコマンドを保管するか設定する。
  • 0 設定を保存してメニューに戻る。
  • q 設定を破棄してメニューに戻る。

オートコンプリートの設定

The new completion system (compsys) allows you to complete commands, arguments and special shell syntax such as variables.  It provides completions for a wide range of commonly used commands in most cases simply by typing the TAB key.  Documentation is in the zshcompsys manual page.
If it is not turned on, only a few simple completions such as filenames are available but the time to start the shell is slightly shorter.

You can:
  (1)  Turn on completion with the default options.

  (2)  Run the configuration tool (compinstall).  You can also run
       this from the command line with the following commands:
        autoload -Uz compinstall
        compinstall
       if you don't want to configure completion now.

  (0)  Don't turn on completion.

--- Type one of the keys in parentheses --- 
  • 1 オートコンプリートを有効化する。
  • 2 compinstall を実行する。
  • 0 オートコンプリートを無効にする。

キーバインドの設定

コマンド入力中にエディタを立ち上げたときのキーバインドを設定する。Emacs風にするかVi風にするか決められる。デフォルト設定ではEmacs風になる。

Default editing configuration
=============================

The keys in the shell's line editor can be made to behave either like Emacs or like Vi, two common Unix editors.  If you have no experience of either, Emacs is recommended.  If you don't pick one, the shell will try to guess based on the EDITOR environment variable.
Usually it's better to pick one explicitly.

# (1) Change default editing configuration
bindkey -e                                                                (not yet saved)

# (0)  Remember edits and return to main menu (does not save file yet)
# (q)  Abandon edits and return to main menu

--- Type one of the keys in parentheses --- 
  • 1 キーバインドの変更。
  • 0 設定を保存してメニューに戻る。
  • q 設定を破棄してメニューに戻る。

シェルオプションの設定

zshにはシェルオプションというユーザの入力に対する振る舞いを決める設定が多く存在する。これらのよく使う設定をこのウィザードでは簡単に行える。

Common shell options
====================

The following are some of the shell options that are most often used.
The descriptions are very brief; if you would like more information,
read the zshoptions manual page (type "man zshoptions").

# (1) Change directory given just path.
# unsetopt autocd                                                (no value set)
# (2) Use additional pattern matching features.
# unsetopt extendedglob                                          (no value set)
# (3) Unmatched patterns cause an error.
# setopt nomatch                                                 (no value set)
# (4) Beep on errors.
# setopt beep                                                    (no value set)
# (5) Immediately report changes in background job status.
# unsetopt notify                                                (no value set)

# (0) or (q)  Return to main menu (no changes made yet)

--- Type one of the keys in parentheses --- 
  • 1  auto cd という名の通りパスのみを入力した場合そのディレクトリに移動する。
  • 2 拡張globを使用する。
  • 3  パターンにマッチしない場合エラーを出力する
  • 4  エラーが発生したときにビープ音を出す。 
  • 5  バッググランドジョブで変化があった場合すぐに通知する。
  • 0 設定を保存してメニューに戻る。
  • q 設定を破棄してメニューに戻る。

よくわからない場合はとりあえず Recommended の項目をウィザードで設定すれば問題は起きないはず。最後にメインメニューで設定を保存することをお忘れずに。

設定をあとから変更したいときはホームディレクトリ内の .zshrc を編集すれば良い。

Zshをインストールし一段落ついたらターミナルの見た目を変えたい人もいるだろう。そんなときに「Powerlevel10k」というテーマを導入することで簡単に変身させることができる。よろしければ下のカードからどうぞ。

Photo by Mohammad Rahmani on Unsplash

コメント

タイトルとURLをコピーしました