使用 Oh My Posh 设置自定义提示符

slug
ohmyposh-powershell-wsl
tags
ohmyposh
powershell
wsl
date
Aug 17, 2024
summary
Oh My Posh 是适用于任何 shell 的自定义提示引擎,支持使用内置主题和自定义主题。
status
Published
type
Post
Oh My Posh 是适用于任何 shell 的自定义提示引擎,支持使用内置主题和自定义主题,能够为 GIT 提供状态颜色编码和自定义提示符支持。

1. 安装 Nerd Font

自定义命令提示符通常使用字形来设置样式,若要在终端中查看所有字形,则需要安装 Nerd Font
Nerd Font 是带图标的字库,能够从 Font Awesome、Devicons、Octicons 等图标库中添加了大量额外的字形。
# 从列表选择 oh-my-posh font install # 直接安装 MesloLGM Nerd 字体 oh-my-posh font install meslo
LGM 代表标准间距大小, Mono 用于终端,Propo 用于GUI界面,

2. 安装 Oh My Posh

2.1. 安装 Oh My Posh for PowerShell

# 自动安装 # <https://github.com/JanDeDobbeleer/oh-my-posh/releases/> winget install JanDeDobbeleer.OhMyPosh -s winget # 手动添加环境变量 $env:Path += ";C:\\Users\\user\\AppData\\Local\\Programs\\oh-my-posh\\bin"

2.2. 安装 Oh My Posh for WSL

curl -s <https://ohmyposh.dev/install.sh> | bash -s

3. 选择并应用 Oh My Posh 主题

可在 Oh My Posh 主题页面 浏览主题的完整列表。

3.1. 应用 PowerShell 提示符主题

首先,修改 PowerShell 执行策略,将 PowerShell 执行策略设置为不受限制。
Get-ExecutionPolicy -List # list set-ExecutionPolicy -ExecutionPolicy Unrestricted # add Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope LocalMachine # remove
选择主题,并更新 PowerShell 配置文件。
# 首次运行,创建配置文件 new-item -type file -path $profile -force # 编辑 # C:\\Users\\WangYan\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1 code $PROFILE # 设置主题 # 将 jandedobbeleer 替换为你选择的主题。 oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\\jandedobbeleer.omp.json" | Invoke-Expression

3.2. 应用 WSL 提示符主题

修改配置文件 .profile 应用主题
cat >> ~/.profile << EOF eval "$(oh-my-posh init bash --config /home/wangyan/.cache/oh-my-posh/themes/jandedobbeleer.omp.json)" EOF
参考文档:
 
If you have any questions, please contact me.