oh-my-posh for Windows

配置流程

参考 [1]。Installation → Windows → manual,复制指令进终端:

1
Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1'))

安装 oh-my-posh 并下载主题。

装好后会自动配置环境变量 POSH_THEMES_PATH,是各种主题的 JSON 文件所在路径,以后可以通过 $env:POSH_THEMES_PATH 调用。

然后开始配置主题,首先查看 PowerShell 配置文件路径:

1
2
3
# powershell profile script
# 这个文件会在每次启动终端时调用
$PROFILE

得到一个后缀为 .ps1 的文件路径。我们希望每次启动 PowerShell时,都可以加载 oh-my-posh 对页面进行美化。编辑该文件:

1
2
# 如果提示路径不存在,手动创建一下即可
notepad $PROFILE

输入如下指令:

1
2
3
4
# 第一条不加应该也可以。笔者删文件时不小心把 PSReadLine 也删了,后来还原的
Import-Module PSReadLine
# $env:POSH_THEMES_PATH\<theme_name.json>,去上文提到的环境变量里看看,以 powerline 为例
oh-my-posh init pwsh --config $env:POSH_THEMES_PATH\powerline.omp.json | Invoke-Expression

保存后重启 PowerShell,已经初步看到美化效果,但 Icon 部分还是乱码。如果之前没有配置 Set-ExecutionPolicy,会提示配置无法加载。

字体

Nerd Fonts 任选字体下载,解压后全选安装在系统字体库,路径为 C:\Windows\Fonts。然后修改 Windows Terminal 配置 (不是 PowerShell 的配置),在终端窗口设置里选中 PowerShell → 外观 → 字体,或直接修改外观配置文件,路径为

1
%LOCALAPPDATA%\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json

参考 [2],这个文件中我们主要关注如下结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{	
...
"profiles":
{
"defaults":
{
"font":
{
// 字体名称去字体文件夹里找,或者在官网
// 如"face": "CodeNewRoman Nerd Font"
"face": "<FontName>",
},
},
"list": ...

找到 "defaults": {},手动添加 font 字段,配置后保存。这时终端能正常加载主题和字体了。

至此,oh-my-posh for PowerShell 配置完成。

背景

设置终端背景,同样在 defaults 下加入以下字段:

1
2
3
4
"backgroundImage": "<imgpath>",
"backgroundImageAlignment": "bottomRight",
"backgroundImageOpacity": 0.3,
"backgroundImageStretchMode": "uniformToFill"

oh my posh for Linux

参考 [3] 安装。因为我在一台 ARM 设备上安装,而 Homebrew 暂不支持 ARM64,故采用手动安装。

1
2
3
# 注意把 posh-linux-arm64 换成当前设备的架构
sudo wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-arm64 -O /usr/local/bin/oh-my-posh
sudo chmod +x /usr/local/bin/oh-my-posh

下面是重新在 Linux 下安装了一遍主题库,这一步可以省略

1
2
3
4
5
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

配置 .bashrc 文件。在 .profile 里配置也是可行的。[4]

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

如果没有重装主题库,则路径改为

1
/mnt/<Windows 下的主题路径>/powerline.omp.json

字体设置同 Windows PowerShell。重启 Bash,成功看到美化效果。




黑夜来了,我驶进银河的港湾。


  1. 📚 oh-my-posh docs ↩︎

  2. Windows终端中的外观配置文件设置 ↩︎

  3. Oh My Posh 文档中的 Linux 安装指南 ↩︎

  4. linux关于bashrc与profile的区别 ↩︎