跳转至

Windows配置oh-my-posh

概要: 配置堪比 oh-my-zsh 的Windows PWSH神器,oh-my-posh

创建时间: 2022.03.20 13:58:30

更新时间: 2023.08.16 22:33:25

注意:以下命令均在Windows Terminal的powershell环境下执行,建议首先配置好Scoop环境

安装oh-my-posh

oh-my-posh近期的安装方式有变化,详见 PowerShell module | Oh My Posh

使用 Scoop 工具

安装

PowerShell
scoop install https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json
升级
PowerShell
scoop update oh-my-posh

使用 Winget 工具

安装

PowerShell
winget install JanDeDobbeleer.OhMyPosh -s winget
更新
PowerShell
winget upgrade JanDeDobbeleer.OhMyPosh -s winget

安装字体

需要先使用 Scoop 工具安装 sudo 软件包,以便在终端中调起Windows的管理员提权对话框

PowerShell
scoop install sudo
然后选择字体安装
PowerShell
sudo oh-my-posh font install
image.png
image.png

创建/修改powershell启动配置文件

PowerShell
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
notepad $PROFILE
安装powershell必要的包
PowerShell
Install-Module posh-git -Scope CurrentUser -Force -AllowClobber

写入如下内容,以在启动powershell时应用oh-my-posh环境

PowerShell
Import-Module posh-git
oh-my-posh init pwsh --config ~/.custom.omp.json | Invoke-Expression
效果如下
image.png

使用oh-my-posh

查看当前shell

PowerShell
oh-my-posh get shell
image.png

显示所有可用的主题以及效果

PowerShell
Get-PoshThemes

设置主题

此处我们将omp的主题文件统一放到 ~/.config/oh-my-posh/themes 文件夹中,使用 ys主题作为默认

PowerShell
1
2
3
4
mkdir -p ~/.config/oh-my-posh/themes
cd ~/.config/oh-my-posh/themes
wget https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/ys.omp.json
oh-my-posh init pwsh --config ~/.config/oh-my-posh/themes/ys.omp.json | Invoke-Expression
image.png

查看主题路径

PowerShell
$env:POSH_THEMES_PATH

参考

  1. 主页:Introduction | Oh My Posh
  2. 安装指引:PowerShell module | Oh My Posh
  3. Windows配置:Windows | Oh My Posh
  4. 主题文件:oh-my-posh/ys.omp.json at main · JanDeDobbeleer/oh-my-posh · GitHub
  5. Windows Terminal美化+PowerShell插件配置 - DiaosSama's Blog
  6. JanDeDobbeleer/oh-my-posh: A prompt theme engine for any shell.
  7. Windows Terminal - PowerShell customization via oh-my-posh/posh-git Set-Theme error? · Issue #9237 · microsoft/terminal