社内SEの話

日々起きたことの記録用

【Powershell】OhMyPoshを使ってモダンなターミナルにする

↓プログラミングで副業を考えたらこちら↓

OhMyPoshでターミナル画面の変更方法について解説

Oh My Poshとは

ohmyposh.dev

OhMyPoshとはPowershellターミナルコンソールを装飾して、より見やすく個人の好みにあったデザインにできるソフトになります。

今回はなるべくPowershellだけで完結できるようにしました。

下準備

下準備時点ではPowershell5を使用しています。

OhMyPoshはPowershell7なので、5は使えないので注意が必要です。

wingetのインストール

# バージョンを取得
$latestVersion = (Invoke-RestMethod -Uri "https://api.github.com/repos/microsoft/winget-cli/releases/latest").tag_name
# インストーラーダウンロード
Invoke-WebRequest -Uri "https://github.com/microsoft/winget-cli/releases/download/$latestVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -OutFile
$env:userprofile\appdata\local\temp\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle

インストール

Add-AppPackage -Path $env:userprofile\appdata\local\temp\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle

バージョン確認

winget -v

ohmyposh

Powershell 7のインストール

winget install Microsoft.PowerShell

ohmyposh

Windows Terminalのインストール

今回インストールするパッケージの確認

winget search 9N0DX20HK701

ohmyposh

winget install 9N0DX20HK701

ohmyposh

場合によっては同意を求められるのでYで同意します

ohmyposh

ターミナルとPowershell7のインストールが完了しました。

これからはOhMyPoshのインストールは管理者モードで実行します

ohmyposh

ohmyposh

OhMyPoshのインストール

winget install JanDeDobbeleer.OhMyPosh

ohmyposh

Oh My Posh 起動設定

設定ファイルの作成

New-Item -Path $PROFILE -Type File -Force
'oh-my-posh init pwsh | Invoke-Expression' | Add-Content -Path $PROFILE

ohmyposh

ターミナルを再起動させるとOhMyPoshが自動で起動します。

ohmyposh

上図はすでにフォントの指定をしているので文字化けしていませんが、起動した直後は文字化けするので、フォントの設定をします

フォントの指定

oh-my-posh font install

ohmyposh

インストールしたいフォントを指定します。

今回はIBMPlexMonoにしました。

ターミナルから Ctrl+Shift + , (カンマ)の同時押しでSetting.jsonを開きます

ohmyposh

フォントの設定をDefaultsの中に設定します

"font": {"face": " BlexMono Nerd Font"}

ohmyposh

メモ帳を保存するとフォントが変更され、文字化けが解消されます。

ohmyposh.dev

テーマの変更

Get-PoshThemesThemes | Oh My Posh から気に入ったテーマを見つけます。

テーマ名から変更する場合

New-Item -Path $PROFILE -Type File -Force
"oh-my-posh init pwsh --config '$env:POSH_THEMES_PATH/<テーマ名>.omp.json' | Invoke-Expression" | Add-Content -Path $PROFILE

例)
New-Item -Path $PROFILE -Type File -Force
"oh-my-posh init pwsh --config '$env:POSH_THEMES_PATH/wholespace.omp.json' | Invoke-Expression" | Add-Content -Path $PROFILE

Githubのテーマから参照する場合

New-Item -Path $PROFILE -Type File -Force
'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/jandedobbeleer.omp.json' | Invoke-Expression " | Add-Content -Path $PROFILE

ohmyposh

テーマが変わります

ohmyposh

テーマのカスタマイズは細かくできるため、内容についてはドキュメントを参考にしてください。