PowerShell 美化——oh-my-posh 的配置与使用

1. 引言

使用 Windows 系统的时候,原本很少有需要终端交互的场景,cmd 工具已经可以很好的应付日常需求了。然而自从经常需要 ssh 到服务器或者开发板上进行操作的时候,基础了 cmd 乃至 powershell 界面就有些简陋了。
笔记将记录使用 oh-my-posh 美化 PowerShell 终端的过程,并备份一下配置文件,方便在别的地方部署。

自定义效果

2. 安装准备

  1. Powershell
    一般现在的 Windows 系统是会预装 PowerShell 的,没有的话可以通过微软商店安装。
  2. Windows Terminal
    推荐通过微软商店直接安装,搜索 Windows Terminal 即可。
  3. oh-my-posh
    有两种安装方式,第一种也是通过微软商店直接安装,搜索 oh-my-posh 即可。
    也可以通过命令行安装,在 Powershell 终端运行即可
Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1'))
  1. 字体文件
    Windows 系统自带的字体有些对特殊字符的支持不太好,官方推荐使用 Meslo LGM NF 字体。
    字体下载网站:Nerd Fonts
    下载后解压,右键->为所有用户安装。

3. 配置过程

3.1 oh-my-posh 初始化

首先更改 Powershell 的安全策略。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

打开 / 新建终端配置文件。

notepad $PROFILE        # 用记事本打开配置文件

# 如果没有此文件,则手动创建
New-Item -Path $PROFILE -Type File -Force

重新打开空白配置文件后,在首行添加下面代码,使得每次打开终端都会加载 oh-my-posh 主题,代码为默认主题。

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/jandedobbeleer.omp.json" | Invoke-Expression

重启终端或者运行 . $PROFILE 即可加载默认主题。

3.2 主题相关操作

完成基础默认配置后,可以开始我们的主题操作了。
oh-my-posh 提供大量的主题样式,可以通过官网或者终端查看:
– 官网主题
Themes | Oh My Posh
– 终端查看

 Get-PoshThemes

选择好心仪的主题后,记住主题名,以 1_shell 主题举例,notepad $PROFILE 打开配置文件,将代码修改为如下(修改默认主题名为新主题名即可):

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/1_shell.omp.json" | Invoke-Expression

重新加载主题便修改成功了。

3.3 配置文件自定义

很多时候喜欢的主题也不是完美符合自己的需求,此时我们就可以自定义我们的主题文件。
oh-my-posh 的主题文件一般存放在如下路径:

C:\Users\<用户名>\AppData\Local\Programs\oh-my-posh\themes

# 也可通过如下命令获得路径
$env:POSH_THEMES_PATH

1_shell 主题为例,进入文件夹路径,找到 1_shell.omp.json 直接进行编辑,或者复制主题文件为新文件 1_shell_self.omp.json 并进行修改。
更推荐第二种做法,防止配置错误导致原主题损坏。

更改完毕后,修改 powershell 配置文件首行为

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/1_shell_self.omp.json" | Invoke-Expression

重启终端即可加载自定义主题。

4. 配置文件备份

Microsoft.PowerShell_profile.ps1

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/1_shell_self.omp.json" | Invoke-Expression
#Thems
#di4am0nd_self
#powerlevel10k_lean
#neko
#robbyrussell
#sim-web
#tokyonight_storm
#sorin
#spaceship
#1_shell

1_shell_self.omp.json

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "alignment": "left",
      "newline": true,
      "segments": [
        {
          "foreground": "#FFBD00",
          "leading_diamond": "<#CB4B16> \ue200 </>",
          "properties": {
            "display_host": true
          },
          "style": "diamond",
          "template": "lichen <#ffffff>on</>",
          "type": "session"
        },
        {
          "foreground": "#bc93ff",
          "properties": {
            "time_format": "Monday <#ffffff>at</> 3:04 PM"
          },
          "style": "diamond",
          "template": " {{ .CurrentDate | date .Format }} ",
          "type": "time"
        },
        {
          "foreground": "#ee79d1",
          "properties": {
            "branch_icon": "\ue725 ",
            "fetch_stash_count": true,
            "fetch_status": true,
            "fetch_upstream_icon": true,
            "fetch_worktree_count": true
          },
          "style": "diamond",
          "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"
        }
      ],
      "type": "prompt"
    },
    {
      "alignment": "right",
      "segments": [
        {
          "foreground": "#a9ffb4",
          "style": "plain",
          "type": "text"
        },
        {
          "foreground": "#a9ffb4",
          "properties": {
            "style": "dallas",
            "threshold": 0
          },
          "style": "diamond",
          "template": " {{ .FormattedMs }}s <#ffffff>\ue601</>",
          "type": "executiontime"
        },
        {
          "properties": {
            "root_icon": "\uf292 "
          },
          "style": "diamond",
          "template": " \uf0e7 ",
          "type": "root"
        },
        {
          "foreground": "#94ffa2",
          "style": "diamond",
          "template": " <#ffffff>MEM:</> {{ round .PhysicalPercentUsed .Precision }}% ({{ (div ((sub .PhysicalTotalMemory .PhysicalFreeMemory)|float64) 1073741824.0) }}/{{ (div .PhysicalTotalMemory 1073741824.0) }}GB)",
          "type": "sysinfo"
        }
      ],
      "type": "prompt"
    },
    {
      "alignment": "left",
      "newline": true,
      "segments": [
        {
          "foreground": "#FFBD00",
          "leading_diamond": "<#00c7fc> \ue285 </><#FFBD00>{</>",
          "properties": {
            "folder_icon": "\uf07b",
            "folder_separator_icon": " \uebcb ",
            "home_icon": "home",
            "style": "agnoster_full"
          },
          "style": "diamond",
          "template": " \ue5ff {{ .Path }} ",
          "trailing_diamond": "<#FFBD00>}</>",
          "type": "path"
        },
        {
          "foreground": "#A9FFB4",
          "foreground_templates": ["{{ if gt .Code 0 }}#ef5350{{ end }}"],
          "properties": {
            "always_enabled": true
          },
          "style": "plain",
          "template": " \ue286 ",
          "type": "status"
        }
      ],
      "type": "prompt"
    }
  ],
  "console_title_template": "{{ .Folder }}",
  "transient_prompt": {
    "background": "transparent",
    "foreground": "#FEF5ED",
    "template": "\ue285 "
  },
  "version": 2
}