ターミナルのプロンプトをかっこよくする! | Oh My Posh

この記事は約15分で読めます。

ターミナルのプロンプトはデフォルトではそっけない感じですよね。もちろんPS1変数をいじればいろいろできますが、手動で設定するのも大変です。そこで今回は Oh My Poshというツールを紹介します。

Home | Oh My Posh
A prompt theme engine for any shell.

いろいろな設定ができますが、のちほど紹介するサンプルでは、通常のディレクトリはこんな感じで処理時間や実行結果がひと目でわかるようになっています。

さらにGitHubのリポジトリディレクトリではこんな感じ。GitHubアイコンとブランチ名、ステータスがわかるようになります。

Python仮想環境だとバージョンが表示されたりもします。

確認環境は WSL上のUbuntuおよび素のUbuntuです。バージョンは下記の通り。

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu 
DISTRIB_RELEASE=24.04
DISTRIB_CODENAME=noble
DISTRIB_DESCRIPTION="Ubuntu 24.04.2 LTS"
スポンサーリンク

導入方法

インストール

まずはインストールします。公式ドキュメントにインストール方法の記載あります。特に難しいことはないです。

Linux | Oh My Posh
Set up your terminal

Linuxなら下記のとおりです(2025-06-22現在)。

$ curl -s https://ohmyposh.dev/install.sh | bash -s
$ which oh-my-posh
/home/shinya/bin/oh-my-posh
スポンサーリンク

NERD FONTS のダウンロード

最初の例でも示したようにGitHubやPythonのアイコンも表示可能です。それらに対応したフォントが必要です。ここでは開発者向けにアイコンを追加したフォント群のNERD FONTS を利用します。

お好みで選べばOKですが、今回の例では日本語もいい感じに表示してくれるBizin Gothicをしています。

日本人プログラマー向けコーディングフォント「Bizin Gothic」が無償公開/「Ricty」でもお馴染みの「Inconsolata」と読みやすい「BIZ UDゴシック」をかけ合わせ
「BIZ UDゴシック」と「Inconsolata」をかけ合わせた日本人プログラマー向けのコーディングフォント「Bizin Gothic」(ビジン ゴシック)が5月28日、試験的にリリースされた。現在、「GitHub」のリリースページから無...

フォントのインストールはお使いのOSに合わせてやってください。通常と変わらない方法でOKです。よくわからなければググれば出てきます。

ちなみにWSLでのフォントの指定は下記のようにできます。今回は Bizin Gothic Discord NFを使用しています。NFは NARD FONTSの略です。

テーマのダウンロード

公式サイトにたくさんのテーマが置いてあります。下記の手順でダウンロードします。

mkdir ~/.poshthemes
wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/themes.zip -O ~/.poshthemes/themes.zip
unzip ~/.poshthemes/themes.zip -d ~/.poshthemes
chmod u+rw ~/.poshthemes/*.omp.*
rm ~/.poshthemes/themes.zip

テーマの反映

下記のコマンドにてテーマのjsonファイルを指定すれば反映されます。~/.poshthemes/ 以下にたくさんテーマがあるのでお好みのものを指定すればOKです。

eval "$(oh-my-posh init bash --config ~/.poshthemes/hoge.json)"

参考に、試しにいくつか設定してみた例を示します。コマンド実行すると即時で反映されてプロンプトが変化します。いろいろなjsonファイルで試してお気に入りを見つけてみてください。

また、下記のサイトにもテーマの表示例があるのでそちらも参考になると思います。
https://ohmyposh.dev/docs/themes

スポンサーリンク

カスタマイズ方法

既存のテーマで飽き足らない場合は自分でカスタマイズすることも可能です。下記の公式サイトの説明に沿ってjsonファイルを修正すればよいです。
結構大変かもしれませんが凝りたい人はやってみてください。

General | Oh My Posh
Oh My Posh renders your prompt based on the definition of blocks (like Lego) which contain one or more segments.

NERD FONTSのユニコードの確認

テーマのjsonファイル内で、NERS FONTS のアイコンはユニコードで指定されています。
例えばPythonのアイコンは \ue235 となっています。このユニコードだけみてもどんなアイコンかわからないので下記のサイトのチートシートを使うと便利です。

Nerd Fonts - Iconic font aggregator, glyphs/icons collection, & fonts patcher
Iconic font aggregator, collection, & patcher: 9,000+ glyph/icons, 60+ patched fonts: Hack, Source Code Pro, more. Popul...

たとえば \ue235を入力すると下記のようにPythonのアイコンが表示されます(ユニコードエスケープシーケンス \u は省きます)。

その他は公式サイト見ながらカットアンドトライやればできると思います。頑張ってください。

テーマのサンプル

参考までに手元でカットアンドトライしてみたテーマのjsonを貼っておきます。冒頭にて紹介した表示例になります。
もとのテーマファイルがzsh向けだったのをbashむけにちょこちょこ修正したものです、細かいところはまちがっているかもなのであくまで参考までです。


{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "alignment": "left",
      "segments": [
        {
          "background": "#d3d7cf",
          "foreground": "#000000",
          "leading_diamond": "\ue0b2",
          "style": "diamond",
          "template": " {{ if .WSL }}WSL{{ end }}{{.Icon}} ",
          "type": "os"
        },
        {
          "background": "#91ddff",
          "foreground": "#100e23",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "home_icon": "~",
            "style": "full"
          },
          "style": "powerline",
          "template": " \uf07c {{ .Path }} ",
          "type": "path"
        },
        {
          "background": "#4e9a06",
          "background_templates": [
            "{{ if or (.Working.Changed) (.Staging.Changed) }}#ff9966{{ end }}",
            "{{ if and (gt .Ahead 0) (gt .Behind 0) }}#f26d50{{ end }}",
            "{{ if gt .Ahead 0 }}#89d1dc{{ end }}",
            "{{ if gt .Behind 0 }}#4e9a06{{ end }}"
          ],
          "foreground": "#000000",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "branch_icon": "\uf126 ",
            "fetch_stash_count": true,
            "fetch_status": true,
            "fetch_upstream_icon": true
          },
          "style": "powerline",
          "template": " {{ .UpstreamIcon }} {{ .HEAD }} {{if .BranchStatus }}{{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ",
          "type": "git"
        },
        {
          "background": "#689f63",
          "foreground": "#ffffff",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "fetch_version": true
          },
          "style": "powerline",
          "template": " {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }} \ue718 ",
          "type": "node"
        },
        {
          "background": "#00acd7",
          "foreground": "#111111",
          "invert_powerline": true,
          "powerline_symbol": "\ue0b0",
          "properties": {
            "fetch_version": true
          },
          "style": "powerline",
          "template": " {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} \ue627 ",
          "type": "go"
        },
        {
          "background": "#4063D8",
          "foreground": "#111111",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "fetch_version": true
          },
          "style": "powerline",
          "template": " {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} \ue624 ",
          "type": "julia"
        },
        {
          "background": "#FFDE57",
          "foreground": "#111111",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "display_mode": "files",
            "fetch_virtual_env": false
          },
          "style": "powerline",
          "template": " {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} \ue235 ",
          "type": "python"
        },
        {
          "background": "#AE1401",
          "foreground": "#ffffff",
          "invert_powerline": true,
          "powerline_symbol": "\ue0b2",
          "properties": {
            "display_mode": "files",
            "fetch_version": true
          },
          "style": "powerline",
          "template": " {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} \ue791 ",
          "type": "ruby"
        },
        {
          "background": "#FEAC19",
          "foreground": "#ffffff",
          "invert_powerline": true,
          "powerline_symbol": "\ue0b2",
          "properties": {
            "display_mode": "files",
            "fetch_version": false
          },
          "style": "powerline",
          "template": " {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} \uf0e7",
          "type": "azfunc"
        },
        {
          "background_templates": [
            "{{if contains \"default\" .Profile}}#FFA400{{end}}",
            "{{if contains \"jan\" .Profile}}#f1184c{{end}}"
          ],
          "foreground": "#ffffff",
          "invert_powerline": true,
          "powerline_symbol": "\ue0b2",
          "properties": {
            "display_default": false
          },
          "style": "powerline",
          "template": " {{ .Profile }}{{ if .Region }}@{{ .Region }}{{ end }} \ue7ad ",
          "type": "aws"
        },
        {
          "background": "#ffff66",
          "foreground": "#111111",
          "invert_powerline": true,
          "powerline_symbol": "\ue0b2",
          "style": "powerline",
          "template": " \uf0ad ",
          "type": "root"
        },
        {
          "background": "#a9a9a9",
          "foreground": "#000000",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "always_enabled": true
          },

          "style": "powerline",
                        "template": " {{ .FormattedMs }}",
          "trailing_diamond": "\ue0b0",
          "type": "executiontime"
        },
        {
          "background": "#007ACC",
          "background_templates": [
            "{{ if gt .Code 0 }}#cc2222{{ end }}"
          ],
          "foreground": "#d3d7cf",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "always_enabled": true
          },
          "style": "powerline",
          "template": " {{ if gt .Code 0 }}\uf00d{{ else }}\uf00c{{ end }} ",
          "type": "status"
        }
      ],
      "type": "prompt"
    }
  ],
  "console_title_template": "{{ .Shell }} in {{ .Folder }}",
  "final_space": true,
  "version": 3
}
スポンサーリンク

まとめ

今回はプロンプトをかっこよくする Oh My Posh を紹介しました。プロンプトをビジュアライズして実行時間や実行結果、Gitのステータスなどをわかりやすく表示できます。カスタマイズ性もあるのでご自分の好みに合わせてかっこよくしてみてください!

コメント