ぼくがかんがえたさいきょうのターミナル環境を作る

2021/03/08に公開

こんなのをめざすよ


前提とか


Powerline対応フォントインストール

zpreztoPowerLevel10kテーマを使うので、それに対応したやつを入れる

romkatv/powerlevel10k から以下をお好みで。


Windows Terminalの設定いじる

それでも良ければどうぞ!

Ctrl+,してsettings.jsonを編集。
各パラメータ値はお好みでいじってください

直下のとこ

settings.json
"defaultProfile": "Ubuntu-20.04",

"initialRows": 50,
"initialCols": 200,

profiles/defaultsの中

settings.json
"startingDirectory": ".",

"fontFace": "MesloLGS NF",
"fontSize": 9,
"antialiasingMode": "cleartype",

"useAcrylic": true,
"acrylicOpacity": 0.95

profiles/list/Ubuntuの中

開始位置をWSL側のホームディレクトリにする

settings.json
// {user-name}にはWSLのユーザー名
"startingDirectory": "//wsl$/Ubuntu-20.04/home/{user-name}",

いざWSLへ

とりあえず更新

bash
sudo apt update
sudo apt upgrade

sudoをパスワードなしでできるように

WSLだと面倒なだけだし、ね

bash
sudo visudo

して

visudo
- %sudo   ALL=(ALL:ALL) ALL
+ %sudo   ALL=(ALL:ALL) NOPASSWD:ALL

sudoeditがnanoなのは嫌なの

sudo update-alternatives --config editor

タブ補完くっそ重いので、Windows側のパスを排除する

/etc/wsl.conf
[interop]
appendWindowsPath = false

設定後、WSLを再起動


Homebrewインストール

公式通りに入れて

bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

インストール後の指示に従う。

bash
sudo apt install build-essential
bash
echo 'eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)' >> ~/.profile
eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
bash
brew install gcc

cleanupとかdoctorとかしておく

bash
brew cleanup
brew doctor

zshインストール

bash
brew install zsh

デフォルトシェルを切り替える

bash
which zsh | sudo tee -a /etc/shells
chsh -s `which zsh`

したあと、Windows Terminal立ち上げ直す

zshの初期設定

zshの初期起動時に、以下の様なメッセージが表示され、初期設定ウィザードが出てきてくれる。
今回はpreztoを入れるので、ここでは即Quit

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

preztoインストール

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

おわったら

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

zshにもHomebrewのパスとおす

zsh
echo 'eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)' >> ~/.zprofile
eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)

ここで一旦シェル再ログイン(Windows Terminalを再起動)


Powerline導入

preztoのテーマをPowerLevel10kに変更

zsh
prompt -s powerlevel10k

Powerlevel10kのウィザードが出るのでお好みで。

終わったら、~/.zshrcに以下追記

~/.zshrc
autoload -Uz promptinit
promptinit
prompt powerlevel10k

GitをHomebrewからインストールする(任意)

aptだとバージョン上がらないみたいなので、基本的に最新版を使いたい場合はHomebrewでインストールしましょう
逆に、何らかの理由で最新バージョンを追いたくない、等の場合はスキップしちゃいましょう

zsh
brew install git

git-completionを有効化

~/.zshrc に追記

~/.zshrc
fpath=($(brew --prefix)/share/zsh/site-functions $fpath)

autoload -U compinit
compinit -u

したら source ~/.zshrc

diff-highlightをパス通す

sudo chmod +x /home/linuxbrew/.linuxbrew/share/git-core/contrib/diff-highlight/diff-highlight
sudo ln -s /home/linuxbrew/.linuxbrew/share/git-core/contrib/diff-highlight/diff-highlight /usr/local/bin/diff-highlight

便利なコマンド類をいれる(任意)

zsh
brew install exa bat zsh-syntax-highlighting zsh-completion

以下、それぞれ超ざっくり紹介

exa

lsをいい感じにしてくれるよ

zsh
exa -l -aa -h -@ -m --icons --git --time-style=long-iso --color=automatic --group-directories-first

とかするとちょっと感動するよ
パラメーター等は公式(ogham/exa)のドキュメント等を参照してお好みで

bat

シンタックスハイライトが出来たりするcat
コマンドもそこまで変わらないので、こっちは特にaliasとかはいいかな。

zsh-syntax-highlighting

zshのコマンドをいい感じにハイライトしてくれる。
存在しなかったりパスが通ってなかったりで、実行できないコマンドを入力すると赤文字にしてくれたり、
地味ながらもすごくありがたい存在。

インストールしたら ~/.zshrc

~/.zshrc
source /home/linuxbrew/.linuxbrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

を追記して source ~/.zshrc

zsh-completion

コマンドの入力時に空気読みながら補完してくれたりする。
もうこれがないと生きていけない……


以上です。お疲れさまでした!

Discussion