使用 oh-my-posh 美化 windows terminal,讓其接近oh-my-zsh

本文旨在快速讓你進行美化,少踩一些坑,原文出自我的博客:prettier-windows-terminal-with-oh-my-posh

爲了同 iterm2 下的 oh-my-zsh 保持基本一致,博主用的是 aliens 主題+一些自定義配置調整,最終效果如下,你也可以根據自己需要進行細調

  • 如果你閒麻煩,或者想跟我用一樣的,文末會給出主題配置,直接拿去用即可。
  • 效果圖:

版本說明:

  • 本文撰寫時:
    • oh-my-posh 版本爲 v7.87.0
    • powershell 的版本爲 7.2.3,(最新爲 7.2.4)
    • OS:win10,21H1,19043.1706

注意事項:

1. 安裝時請關閉殺毒軟件、各類管家,否則寫入註冊表時會報錯。
2. 有條件的請掛代理,因爲源文件在 git 上。
3. 全程在 powershell ,管理員模式 下進行。

安裝 windows terminal:

可直接在 microsoft store 中搜索安裝。這裏不贅述。

安裝 powershell@latest

官網:https://docs.microsoft.com/zh-cn/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.2

  • 你的系統中,powershell 可能不是新版,一樣弄,強烈建議使用最新版本的 powershell。
    • 因爲新版會增加不少工具包,如 Test-Path 等,可以省去不少自己去各類安裝包的麻煩。也可以減少、避免不必要的安裝錯誤。

安裝 gsudo

如其名,爲了在 windows 下使用 sudo 命令,需要安裝該包。
git 地址:https://gerardog.github.io/gsudo/docs/install

可以直接在 powershell 中執行以下命令進行安裝:

winget install gerardog.gsudo
  • 會自動註冊到 path,所以你可以直接在命令行中全局使用:
# 示例:以管理員身份啓動 powershell
gsudo powershell.exe

如果你覺得 gsudo 不夠直觀,希望用 sudo,可以通過以下方式重命名:

Set-Alias 'sudo' 'gsudo'

安裝 oh-my-posh

官方:https://ohmyposh.dev/
git 地址:https://github.com/jandedobbeleer/oh-my-posh

winget install oh-my-posh
  • 以上命令會安裝 oh-my-posh.exe 以及最新的主題。

安裝 Nerd Fonts

oh-my-posh 的不少主題都是依賴於該字體,否則部分 icon 會亂碼,建議安裝。

windows terminal 配置

啓動 shell 設置爲 powershell 7

以管理員權限啓動 shell

在 windows terminal 的設置中,打開JSON文件:

編輯配置:

  • 只要安裝了 powershell 7,默認情況下 pwsh 就會指向 powershell 7。
  • 如果你沒有給 gsudo 添加 sudo 別名的話,這裏用 gsudo

設置 powershell 7 字體爲 Nerd fonts

配置啓動時加載 oh-my-posh

創建 powershell 配置文件:

if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }

用記事本打開配置文件

notepad $PROFILE

在記事本中錄入以下指令

oh-my-posh init pwsh --config ~/.leon.omp.theme.json | Invoke-Expression
  • 每次啓動 shell 時 ,會先執行以上命令以加載主題等配置。
  • 其中 "--config ~/.leon.omp.theme.json" 是你的文件和所在路徑,文末我會放出我的配置。
  • 你可以在 這裏 找到不同的主題。

快捷鍵設置:

習慣了 iterm2 中 CMD+D,SHIFT+CMD+D 來拆分窗口,很慶幸 windows terminal 已經提供了快捷鍵修改,且針對的就是拆分窗口,直接設置即可,如下:

.leon.omp.theme.json

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "alignment": "left",
      "segments": [
        {
          "background": "#61AFEF",
          "foreground": "#ffffff",
          "leading_diamond": "\ue0b6",
          "style": "diamond",
          "template": "{{ .HostName }} ",
          "trailing_diamond": "\ue0b0",
          "type": "session"
        },
        {
          "background": "#C678DD",
          "foreground": "#ffffff",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "style": "full"
          },
          "style": "powerline",
          "template": " {{ .Path }} ",
          "type": "path"
        },
        {
          "background": "#95ffa4",
          "foreground": "#193549",
          "powerline_symbol": "\ue0b0",
          "style": "powerline",
          "template": " {{ .HEAD }} ",
          "type": "git"
        },
        {
          "background": "#FF6471",
          "foreground": "#ffffff",
          "leading_diamond": "<transparent,background>\ue0b0</>",
          "style": "diamond",
          "template": " {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}{{ end }} ",
          "trailing_diamond": "\ue0b4",
          "type": "python"
        }
      ],
      "type": "prompt"
    }
  ],
  "final_space": true,
  "version": 2
}
  • 屬性說明你可以從 這裏 瞭解。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章