通用配置
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
Oh My Posh 通过定义类似乐高积木的_块_(blocks)来渲染命令提示符,每个块包含一个或多个_片段_(segments)。
一个简单的配置示例如下。默认格式是 json,同时也支持 toml 和 yaml。
我们提供了实时更新的schema,可帮助实现配置的自动补全和验证。
现有多个预定义的主题配置可直接使用,也可作为创建自定义配置的起点。
- json
- yaml
- toml
{
"$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"
}
}
]
}
]
}
# yaml-language-server: $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: ""
foreground: "#ffffff"
background: "#61AFEF"
options:
style: folder
"$schema" = "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json"
final_space = true
version = 4
[[blocks]]
type = "prompt"
alignment = "left"
[[blocks.segments]]
type = "path"
style = "powerline"
powerline_symbol = ""
foreground = "#ffffff"
background = "#61AFEF"
[blocks.segments.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'
设置项
| Name | Type | Default | Description |
|---|---|---|---|
final_space | boolean | when true adds a space at the end of the prompt | |
pwd | string | notify terminal of current working directory, values can be osc99, osc7 or osc51 depending on your terminal. Supports templates | |
terminal_background | string | color - 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_color | string | color - accent color, used as a fallback when the accent color is not supported | |
var | map[string]any | config variables to use in templates. Can be any value | |
shell_integration | boolean | false | enable shell integration using FinalTerm's OSC sequences. Works in bash, cmd (Clink v1.14.25+), fish, powershell and zsh |
enable_cursor_positioning | boolean | false | enable fetching the cursor position in bash and zsh to allow automatic hiding of leading newlines when at the top of the shell |
patch_pwsh_bleed | boolean | false | patch 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) |
upgrade | Upgrade | enable auto upgrade or the upgrade notice. See Upgrade | |
iterm_features | []string | false | enable iTerm2 specific features:
|
maps | Maps | a list of custom text mappings | |
async | boolean | false | load 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 |
version | int | 4 | the config version, currently at 4 |
extends | string | the configuration to extend from |
映射表
| Name | Type | Description |
|---|---|---|
user_name | object | text replacement mapping for user names |
host_name | object | text replacement mapping for host names |
shell_name | object | text replacement mapping for shell names. This modifies the .Shell global property. |
- json
- yaml
- toml
{
"maps": {
"user_name": {
"jan": "🚀",
"root": "⚡"
},
"host_name": {
"laptop123": "work"
}
}
}
maps:
user_name:
jan: 🚀
root: ⚡
host_name:
laptop123: work
[maps.user_name]
jan = "🚀"
root = "⚡"
[maps.host_name]
laptop123 = "work"
继承配置
extends 键支持扩展现有配置(如主题),避免重复设置。其值应为其他配置文件的路径(支持本地或远程)。
覆盖值只需在新配置中重复对应键。例如要修改基础配置中的 accent_color,在新配置中声明 accent_color 键即可覆盖原值。
覆盖片段或块时,需保持相同的 type 和 alignment 属性。可通过 alias 或 type 匹配片段进行修改或新增,不影响其他配置。
对于更高级的用例,你还可以指定要覆盖的 block 或 segment 的索引。这允许你在配置中覆盖特定位置的 blocks 或 segments。请注意,索引是1起始的,因此第一个 block 或 segment 的索引为 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 支持三种配置文件格式:json、yaml 和 toml。
虽然存在多种格式转换工具,但这些转换并不完美,仍需手动调整。特别需要注意的是,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
转换工具无法自动处理这种差异,因此需要您手动调整配置。