【Ubuntu 24.04】Tab補完で快適に!zsh導入とシェル切り替え手順を解説
Linuxでターミナル操作をしていると、コマンド補完があるだけで作業効率が一気に上がります。
Ubuntuでは、標準のbashでも補完機能はありますが、zsh に切り替えることでより強力で快適な補完が使えるようになります。
この記事では、
- zshの導入方法
- rootとユーザのログインシェル切り替え
- 今どのシェルを使っているかの確認方法
- bashへ戻す方法
までわかりやすくまとめています。
zshインストール
まずはzshをインストールします。
sudo apt update
sudo apt install -y zshログインシェルをzshへ変更
シェルをbashからzshへ変更します。testユーザとrootの両方をzshに変更する例です。
sudo chsh -s /usr/bin/zsh test
sudo chsh -s /usr/bin/zsh rootシェルの反映を確認します。以下のように末尾が/usr/bin/zshと表示されていればOKです。
test@ubuntu:~$ getent passwd test
test:x:1000:1000:test:/home/gumio:/usr/bin/zsh
test@ubuntu:~$ getent passwd root
root:x:0:0:root:/root:/usr/bin/zsh設定変更後、一度ログアウト→再ログインを実施します。
初回ログイン
初回ログイン時に、次のようなメッセージが表示されるので、「2」を入力します。基本的な設定が入った.zshrcを自動で生成してくれます。
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 --- 2 ←入力
/home/gumio/.zshrc:15: scalar parameter HISTFILE created globally in function zsh-newuser-install
(eval):1: scalar parameter LS_COLORS created globally in function zsh-newuser-install
test@ubuntu ~ %動作確認
「ip a」まで入力してTab補完した場合
test@ubuntu ~ % ip addr自動で「ip addr」まで入力してくれます。
もう一度Tabをクリックした場合
test@ubuntu ~ % ip addr
Completing ip command
addrlabel -- manage addrlabel
addr -- manage protocol address候補をリストアップしてくれます。
関連コマンド
ログインシェルをbashに戻すコマンド
sudo chsh -s /bin/bash test
sudo chsh -s /bin/bash root現在のシェルを確認するコマンド
echo $SHELLシェルの設定を確認するコマンド
getent passwd test
getent passwd root設定変更せず、一時的にシェルを切り替えるコマンド
exec bash -l
exec zsh -lTera Term + zshでマウスホイールが上下キー操作になる原因と対処
Tera Termをターミナルとして使用している場合、zshへ切り替えると、マウスホイールの上下操作がスクロールではなく、コマンド履歴の移動(上下キーと同じ動作)になってしまいます。
この挙動は設定を変更することで回避できますが、vimなどでのカーソル移動に影響がでるため、設定変更はあまりおすすめできません。
代替策として、Ctrlキーを押しながらマウスホイールを操作することでスクロールが可能です。特に問題がなければ、こちらの方法で対応するのが良いかと思います。
まとめ
ターミナルを日常的に使う人で、作業効率を上げたいならzshの導入はとてもおすすめです。
- Tab補完が賢くなる
- もし不要になってもbashに簡単に戻せる
まずは試してみるくらいの感覚で導入してみても損はありません。
