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

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

  • [1. 引言](#1. 引言)
  • [2. 安装准备](#2. 安装准备)
  • [3. 配置过程](#3. 配置过程)
    • [3.1 oh-my-posh 初始化](#3.1 oh-my-posh 初始化)
    • [3.2 主题相关操作](#3.2 主题相关操作)
    • [3.3 配置文件自定义](#3.3 配置文件自定义)
  • [4. 配置文件备份](#4. 配置文件备份)

同步发布在个人笔记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 终端运行即可
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 的安全策略。

powershell 复制代码
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

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

powershell 复制代码
notepad $PROFILE		# 用记事本打开配置文件

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

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

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

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

3.2 主题相关操作

完成基础默认配置后,可以开始我们的主题操作了。

oh-my-posh 提供大量的主题样式,可以通过官网或者终端查看:

powershell 复制代码
Get-PoshThemes

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

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

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

3.3 配置文件自定义

很多时候喜欢的主题也不是完美符合自己的需求,此时我们就可以自定义我们的主题文件。

oh-my-posh 的主题文件一般存放在如下路径:

powershell 复制代码
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 配置文件首行为

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

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

4. 配置文件备份

Microsoft.PowerShell_profile.ps1

powershell 复制代码
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

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
}
相关推荐
柳鲲鹏1 天前
编译Windows版本的zlib
windows
Name_NaN_None1 天前
安卓手机远程控制电脑方法(Android/Windows) —— 「小白教程」
android·windows·智能手机·电脑·笔记本
宝桥南山2 天前
Visual Studio - 记录一个Visual Studio 2026(Insiders)的更新问题
windows·microsoft·微软·.net·.netcore·visual studio
JosieBook2 天前
【鸿蒙开源】把 Windows 任务栏上的流量表搬到鸿蒙 PC:TrafficMonitor 1.86 的 C++ 内核移植实践
c++·windows·harmonyos
Vex2une2 天前
把 Windows 装进 U 盘随身携带:Windows To Go 现状与 WinToUSB 制作完全指南
windows·vhdx·windows to go·wintousb·便携windows·系统带进u盘·随身系统
无奈笑天下3 天前
Wine适配Windows程序常见故障标准处理流程
linux·运维·服务器·windows
广东程序猿小展3 天前
PotPlayer 播放 4K 视频卡顿怎么办(硬解与渲染设置)
windows·工具·软件
xx z4 天前
Radmin LAN 怎么联机(虚拟局域网建房与加入教程)
windows·工具·软件
广东程序猿小展4 天前
谷歌相机配置文件怎么导入(GCam 配置导入与不生效排查)
windows·工具·软件