跳转到主内容

通用配置

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

Oh My Posh 通过定义类似乐高积木的_块_(blocks)来渲染命令提示符,每个块包含一个或多个_片段_(segments)。 一个简单的配置示例如下。默认格式是 json,同时也支持 tomlyaml。 我们提供了实时更新的schema,可帮助实现配置的自动补全和验证。

信息

现有多个预定义的主题配置可直接使用,也可作为创建自定义配置的起点。

{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"final_space": true,
"version": 4,
"blocks": [
{
"type": "prompt",
"alignment": "left",
"segments": [
{
"type": "path",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#ffffff",
"background": "#61AFEF",
"options": {
"style": "folder"
}
}
]
}
]
}

此配置会渲染一个powerline片段,显示当前所在文件夹名称。 要结合Oh My Posh可执行文件应用此配置,请使用--config标志指定包含上述代码的配置文件路径。 --shell universal标志用于打印不含转义字符的提示符,可预览其在shell提示函数中的实际显示效果。

注意

以下命令仅临时打印提示符到终端,不会永久修改shell配置。 如需永久使用自定义配置,请修改shell的提示符配置指向你的自定义文件。

oh-my-posh print primary --config sample.json --shell uni

若一切正常,下一行将显示渲染后的提示符。如果出现大量问号方块,请先配置终端使用支持的字体

技巧

--config 标志支持本地文件路径和远程配置文件地址。

例如,以下是一个有效的 --config 标志: --config 'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/jandedobbeleer.omp.json'

设置项

NameTypeDefaultDescription
final_spacebooleanwhen true adds a space at the end of the prompt
pwdstringnotify terminal of current working directory, values can be osc99, osc7 or osc51 depending on your terminal. Supports templates
terminal_backgroundstringcolor - terminal background color, set to your terminal's background color when you notice black elements in Windows Terminal or the Visual Studio Code integrated terminal
accent_colorstringcolor - accent color, used as a fallback when the accent color is not supported
varmap[string]anyconfig variables to use in templates. Can be any value
shell_integrationbooleanfalseenable shell integration using FinalTerm's OSC sequences. Works in bash, cmd (Clink v1.14.25+), fish, powershell and zsh
enable_cursor_positioningbooleanfalseenable fetching the cursor position in bash and zsh to allow automatic hiding of leading newlines when at the top of the shell
patch_pwsh_bleedbooleanfalsepatch a PowerShell bug where the background colors bleed into the next line at the end of the buffer (can be removed when this is merged)
upgradeUpgradeenable auto upgrade or the upgrade notice. See Upgrade
iterm_features[]stringfalseenable iTerm2 specific features:
  • prompt_mark: add the iterm2_prompt_mark function for supported shells
  • current_dir: expose the current directory for iTerm2
  • remote_host: expose the current remote and user for iTerm2
mapsMapsa list of custom text mappings
asyncbooleanfalseload the prompt async. Will either load the standard prompt, or allow you to start typing right away. Supported for pwsh, powershell, zsh, bash and fish
versionint4the config version, currently at 4
extendsstringthe configuration to extend from

映射表

NameTypeDescription
user_nameobjecttext replacement mapping for user names
host_nameobjecttext replacement mapping for host names
shell_nameobjecttext replacement mapping for shell names. This modifies the .Shell global property.
{
"maps": {
"user_name": {
"jan": "🚀",
"root": "⚡"
},
"host_name": {
"laptop123": "work"
}
}
}

继承配置

extends 键支持扩展现有配置(如主题),避免重复设置。其值应为其他配置文件的路径(支持本地或远程)。

覆盖值只需在新配置中重复对应键。例如要修改基础配置中的 accent_color,在新配置中声明 accent_color 键即可覆盖原值。

覆盖片段或块时,需保持相同的 typealignment 属性。可通过 aliastype 匹配片段进行修改或新增,不影响其他配置。

对于更高级的用例,你还可以指定要覆盖的 blocksegment 的索引。这允许你在配置中覆盖特定位置的 blockssegments。请注意,索引是1起始的,因此第一个 blocksegment 的索引为 1

JSON模式校验

如前所述,Oh My Posh 配置可通过JSON Schema校验。配置中应引用外部模式文档规范结构。 编辑器启用JSON Schema支持后,将对比配置与文档并提示差异警告。

例如以下代码:

...
"segments": [
{
"type": "an_invalid_entry",
"template": "{{ if gt .Code 0 }}\uf134{{ end }}",
}
]
...

type 字段会触发警告(因 an_invalid_entry 非有效值),整个片段({}内)也会警告(缺少必填的 style 键)。 请善用这些警告提示,忽略风险自负。

支持格式

Oh My Posh 支持三种配置文件格式:jsonyamltoml

虽然存在多种格式转换工具,但这些转换并不完美,仍需手动调整。特别需要注意的是,JSON 模式的实现方式如下:

{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json"
}

而对于 YAML:

# yaml-language-server: $schema=https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json

转换工具无法自动处理这种差异,因此需要您手动调整配置。