oh-my-posh 是个跨平台的终端提示符美化工具,支持 PowerShell、bash、zsh、fish、cmd。它用 JSON 配置文件定义提示符长什么样,可以显示当前目录、git 分支、执行时间、电池、天气、各种编程语言版本等。

安装

Windows 上最方便的是用 winget:

winget install JanDeDobbeleer.OhMyPosh

装完会多出一个 oh-my-posh.exe。macOS 和 Linux 可以用 Homebrew:

brew install jandedobbeleer/oh-my-posh/oh-my-posh

或者直接用官方安装脚本:

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

字体

oh-my-posh 的很多主题用到 Nerd Font 图标。如果看到方块或乱码,说明当前字体没有这些字形。

推荐装 MesloLGM Nerd Font 或 Cascadia Code PL。Windows Terminal 里在设置 → 外观 → 字体里选上。VS Code 终端同样要在 settings.json 里配 terminal.integrated.fontFamily

初始化

根据你的 shell 加一句启动命令。

PowerShell(加到 $PROFILE):

oh-my-posh init pwsh | Invoke-Expression

bash(加到 ~/.bashrc):

eval "$(oh-my-posh init bash)"

zsh(加到 ~/.zshrc):

eval "$(oh-my-posh init zsh)"

fish(加到 ~/.config/fish/config.fish):

oh-my-posh init fish | source

换主题

初始化时可以直接指定主题:

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

官方内置了很多主题,装完后主题文件一般在:

  • Windows:%LOCALAPPDATA%\Programs\oh-my-posh\themes\
  • macOS/Linux:~/.cache/oh-my-posh/themes/

想试效果可以临时运行:

oh-my-posh print primary --config "C:\path\to\theme.omp.json"

自己改配置

主题就是 JSON。一个最小结构长这样:

{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"version": 2,
"blocks": [
{
"type": "prompt",
"alignment": "left",
"segments": [
{
"type": "path",
"style": "plain",
"foreground": "#26C6DA",
"template": "{{ .Path }} "
},
{
"type": "git",
"style": "plain",
"foreground": "#FFEB3B",
"template": "({{ .HEAD }})"
}
]
}
]
}

segment 类型很多:path、git、executiontime、battery、node、python、go、kubectl……官方文档有完整列表。

多台机器同步

把主题文件放到 git 仓库或网盘里同步,初始化时指向固定路径。或者用环境变量 POSH_THEME 指向配置,这样换机器只需改一个变量。

$env:POSH_THEME = "C:\Users\codem\.config\oh-my-posh\mytheme.omp.json"
oh-my-posh init pwsh --config $env:POSH_THEME | Invoke-Expression

值不值得装

如果你每天大量时间在终端里,一个信息丰富、一眼能看清上下文状态的提示符确实能省不少事。oh-my-posh 配置一次,跨 shell 复用,维护成本不高。