はじめに

本記事では、WSL2のUbuntuにZshをインストールし、Sheldonを用いてプラグインの管理を行う手順を記載します。

Zshは豊富な機能やプラグインがあり、カスタマイズ性が高いシェルです。

また、今回はプラグインマネージャーとしてSheldonを利用します。

SheldonはRust製で高速かつプラグインの管理をtoml形式で記載できるため、管理方法がわかりやすいかなと思い採用してみました。

環境

  • OS:Windows 11 Pro

    • バージョン:23H2
    • OSビルド:22631.4037
  • WSL2

    • Ubuntu 22.04.3

Zshのインストール

apt installでバージョンが参照できたり、/etc/shellsにZshが追加されていたらインストールはOK。

$ sudo apt install zsh
$ zsh --version
zsh 5.8.1 (x86_64-ubuntu-linux-gnu)
$ cat /etc/shells
/bin/zsh
/usr/bin/zsh

ログインシェルの変更

chsh -sでZshを指定することで、ログインシェルを変更することができます。

指定するシェルのパスは先の手順で確認した/etc/shellsに記載されているものが指定できます。

$ chsh -s /bin/zsh

ログアウト/ログインを行いシェルを読み込ませると、以下の通りzshrcの設定を行う対話型のメニューが表示されます。

今回は自分で設定を行うため、qを入力します。

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.

(2)  Populate your ~/.zshrc with the configuration recommended
     by the system administrator and exit (you will need to edit
     the file by hand, if so desired).

--- Type one of the keys in parentheses ---

Sheldonのインストール

次にSheldonをインストールします。

インストール方法は、Sheldonのgithubに記載されているバイナリインストールの手順を利用します。他にもHomebrewやCargo等でもインストールできます。

% curl --proto '=https' -fLsS https://rossmacarthur.github.io/install/crate.sh \
    | bash -s -- --repo rossmacarthur/sheldon --to ~/.local/bin
info: latest release: 0.8.0
info: found valid target: x86_64-unknown-linux-musl
info: downloading: sheldon-0.8.0-x86_64-unknown-linux-musl.tar.gz
######################################################################## 100.0%
info: installed: /home/zshtest/.local/bin/sheldon

これで~/.local/bin配下にSheldonがインストールされました。

~/.local/binにPATHが通っていない場合は、.zshrcや.zshenv等に以下を記載します。

export PATH=$PATH:$HOME/.local/bin

プラグインのインストール

続いて、プラグインのインストールを行います。

まずは、Sheldonの設定ファイルを作成します。

以下のコマンドを実行することで、$XDG_CONFIG_HOME/sheldon配下にplugins.tomlが作成されます。

% sheldon init --shell zsh
Initialize new config file `~/.config/sheldon/plugins.toml`? [y/N] y #yを入力
Initialized ~/.config/sheldon/plugins.toml

作成されたplugins.tomlの[plugins]テーブルにインストールしたいプラグイン名を記載することで、自動的にインストールできます。

plugins.tomlを編集する際は、専用コマンドのsheldon editコマンドか、vim等で直接編集すればOKです。なお、sheldon editコマンドは$EDITORに設定されているエディタが起動します。

例えば、コマンドラインにシンタックスハイライトを付けてくれるzsh-users/zsh-syntax-highlightingをインストールする場合は以下のように記載します。

% vim ~/.config/sheldon/plugins.toml
shell = "zsh"

[plugins]

# 追記 zsh-syntax-highlightingの部分は一意なものを記載
[plugins.zsh-syntax-highlighting]
github = "zsh-users/zsh-syntax-highlighting"

プラグインのソースをインストールし、読み込ませるには.zshrcに以下を記載します。

eval "$(sheldon source)"

上記を記載すると~/.local/share/sheldon/plugins.lockというロックファイルが作成され、そこにプラグインの詳細な情報が記録されます。

.zshrcに記載することでシェルの起動時にロックファイルを読み込み、高速にプラグインを動かすことができます。

.zshrcに追記した上でzshを再度起動すると、zsh-syntax-highlightingが動作してコマンドラインがカラフルになっていることが確認できます。

plugin_installed

プラグインのアップデート、再インストール、削除は以下のコマンドで実行できます。

# アップデート
% sheldon lock --update

# 再インストール
% sheldon lock --reinstall

# 削除 引数にplugins.tomlで記載した一意の名前を渡す
sheldon remove plugin_name

その他の使い方はREADMEに記載されているので併せてご確認ください。

最後に

今回はZshとSheldonを利用したプラグインのインストールまでといった簡単な部分しか記載できていませんが、それでもZshのパワフルさを体験できました。

他にも遅延読み込み等色々とカスタマイズができるみたいなので。試したら都度追記していきたいと思います。

参考にさせていただいたサイト