Skip to content

Instantly share code, notes, and snippets.

@simics-ja
Last active July 17, 2022 22:18
Show Gist options
  • Save simics-ja/c55391d20e096698482c9b0131d98266 to your computer and use it in GitHub Desktop.
Save simics-ja/c55391d20e096698482c9b0131d98266 to your computer and use it in GitHub Desktop.
[WSL2のlinuxbrew+zsh+zplug+PREZTO+Powerline環境セットアップ手順のメモ] #shell #wsl #ubuntu

参考にしたURL

下準備

  • WSL2環境作っておく
    • このメモではUbuntu環境
  • Windows Terminalインストール
  • Powerlineフォントの入手&インストール
    • ここから手に入る.Source Code Proおすすめ.
    • Ricty Diminished for Powerlineがよければここから手に入る.
  • Windows Terminalの設定
    • カラースキームやフォントの設定をしておく.
    "profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles.
            "fontFace": "源ノ角ゴシック Code JP R",
            "fontSize": 12,
            "backgroundImageOpacity": 0.9,
            "colorScheme": "Solarized Dark",
            "useAcrylic": true
        },
        "list":
        [
            {
                // Make changes here to the powershell.exe profile.
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "powershell.exe",
                "hidden": false
            },
            {
                // Make changes here to the cmd.exe profile.
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "コマンド プロンプト",
                "commandline": "cmd.exe",
                "hidden": false
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "hidden": false,
                "name": "Ubuntu",
                "fontFace": "Source Code Pro for Powerline",
                "source": "Windows.Terminal.Wsl"
            }
        ]
    },

Linuxbrewのセットアップ

前もって

sudo apt update
sudo apt upgrade

ひたすらLinuxbrewのドキュメントに従う.

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"
sudo apt install build-essential curl file git

上から順番に実行

test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile

brew doctorで問題が無けれなばOK

zshをセットアップ

brew install zsh
which zsh | sudo tee -a /etc/shells
chsh -s /home/linuxbrew/.linuxbrew/bin/zsh

このままターミナルを起動し直すとbrewが使えなくなるので,以下のコマンドを実行しておく.

echo 'export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"' >> ~/.zshrc

ターミナルを再起動したら,zshが起動する.brewも使える.

zplugからPREZTOをセットアップ

zplugのインストール

brew install zplug
ln -s /home/linuxbrew/.linuxbrew/opt/zplug ~/.zplug

.zshrcに以下を追記して,ターミナルを再起動する.

export ZPLUG_HOME=$HOME/.zplug
source $ZPLUG_HOME/init.zsh
zplug "sorin-ionescu/prezto"
if ! zplug check --verbose; then
  printf 'Install? [y/N]: '
  if read -q; then
    echo; zplug install
  fi
fi

preztoをインストールするか聞かれるのでyを入力してインストールする.

以降の作業の邪魔になるので.zshrcは退避するか消しておく.

ホームディレクトリにpreztoへのシンボリックリンクを作る.

ln -s /home/linuxbrew/.linuxbrew/opt/zplug/repos/sorin-ionescu/prezto ~/.zprezto
setopt EXTENDED_GLOB

を実行してから,念のために以下のコマンドの出力結果を確認する(この手順はスキップ可).

for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
  echo $rcfile
done

出力結果が以下のようになればOK

/home/<username>/.zprezto/runcoms/zlogin
/home/<username>/.zprezto/runcoms/zlogout
/home/<username>/.zprezto/runcoms/zpreztorc
/home/<username>/.zprezto/runcoms/zprofile
/home/<username>/.zprezto/runcoms/zshenv
/home/<username>/.zprezto/runcoms/zshrc

確認できたら,

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

ホームディレクトリにzshの設定ファイル.zlogin.zlogoutzpreztorczprofilezshenvzshrcへのシンボリックリンクができているので以降はこれを編集していく.

LinuxbrewのパスやZPLUG_HOMEが消えているので忘れずに.zshrcに以下を追記しておく.

# Customize to your needs...
# linuxbrew
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"

# zplug
export ZPLUG_HOME=$HOME/.zplug
source $ZPLUG_HOME/init.zsh
zplug "sorin-ionescu/prezto"

if ! zplug check --verbose; then
  printf 'Install? [y/N]: '
  if read -q; then
    echo; zplug install
  fi
fi

.zpreztorcの119行目あたりでテーマを設定できるので,sorinからpowerlineに変更する. ちなみに,他のテーマにしたければprompt -pコマンドで他のテーマを確認できる.

zstyle ':prezto:module:prompt' theme 'powerline'

starshipと組み合わせるときなど,オフにするときは

zstyle ':prezto:module:prompt' theme 'none'

これでターミナルの見た目がかっこよくなります.めでたしめでたし.

zsh設定例

zplugでいろいろプラグインを入れて幸せになりましょう.

# zplug
export ZPLUG_HOME=$HOME/.zplug
source $ZPLUG_HOME/init.zsh
# zsh framework
zplug "sorin-ionescu/prezto"
# prezto modules
zplug "modules/environment", from:prezto
zplug "modules/terminal", from:prezto
zplug "modules/editor", from:prezto
zplug "modules/history", from:prezto
zplug "modules/directory", from:prezto
zplug "modules/git", from:prezto
zplug "modules/spectrum", from:prezto
zplug "modules/utility", from:prezto
zplug "modules/completion", from:prezto
zplug "modules/prompt", from:prezto
zplug "modules/homebrew", from:prezto

# completions
zplug "zsh-users/zsh-autosuggestions"
zplug "zsh-users/zsh-completions"
zplug "zsh-users/zsh-syntax-highlighting", defer:2
zplug "b4b4r07/enhancd", use:init.sh

# zsh-gomi(file deletion utility) with fzf
zplug "junegunn/fzf-bin", \
    as:command, \
    from:gh-r, \
    rename-to:"fzf", \
    frozen:1

zplug "b4b4r07/zsh-gomi", \
    as:command, \
    use:bin/gomi, \
    on:junegunn/fzf-bin

# interactive search
zplug "mollifier/anyframe"
zstyle ":anyframe:selector:" use fzf

# easily cd gitroot
zplug "mollifier/cd-gitroot"

if ! zplug check --verbose; then
  printf 'Install? [y/N]: '
  if read -q; then
    echo; zplug install
  fi
fi

zplug load --verbose

clear

# zsh history optimizer
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_FIND_NO_DUPS
setopt HIST_REDUCE_BLANKS
setopt HIST_NO_STORE

# anyframe keybind
bindkey '^p' anyframe-widget-put-history
bindkey '^r' anyframe-widget-execute-history
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment