自定义oh-my-posh主题Git模块出现问号问题求助
问题:Oh My Posh Git模块未暂存文件时显示问号
我是Oh My Posh新手,正在通过参考、复制其他主题片段拼接自定义主题,当前使用PowerShell和JetBrainsMono Nerd Font。主题整体运行正常,但存在未暂存文件时,Git模块会显示问号。我尝试替换所有Unicode符号但无效,附上相关代码和复现步骤,请求排查原因。
Git段代码
{ "background": "#00C853", "background_templates": [ "{{ if or (.Working.Changed) (.Staging.Changed) }}#FFEB3B{{ end }}", "{{ if and (gt .Ahead 0) (gt .Behind 0) }}#FFCC80{{ end }}", "{{ if gt .Ahead 0 }}#B388FF{{ end }}", "{{ if gt .Behind 0 }}#B388FF{{ end }}" ], "foreground": "#000000", "powerline_symbol": "\ue0b0", "properties": { "fetch_stash_count": true, "fetch_status": true }, "style": "powerline", "template": " {{ .HEAD }}{{ if .Staging.Changed }}<#FF6F00> \uf046 {{ .Staging.String }}</>{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ", "type": "git" }
完整主题JSON
{ "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json", "blocks": [ { "alignment": "left", "segments": [ { "background": "#07585c", "foreground": "#ffffff", "leading_diamond": "\ue0b6", "style": "diamond", "template": " {{ .UserName }}@{{ .HostName }} ", "trailing_diamond": "\ue0b0", "type": "session" }, { "background": "#444444", "foreground": "#E4E4E4", "properties": { "style": "full" }, "style": "diamond", "template": " {{ .Path }} ", "type": "path" }, { "background": "#00C853", "background_templates": [ "{{ if or (.Working.Changed) (.Staging.Changed) }}#FFEB3B{{ end }}", "{{ if and (gt .Ahead 0) (gt .Behind 0) }}#FFCC80{{ end }}", "{{ if gt .Ahead 0 }}#B388FF{{ end }}", "{{ if gt .Behind 0 }}#B388FF{{ end }}" ], "foreground": "#000000", "powerline_symbol": "\ue0b0", "properties": { "fetch_stash_count": true, "fetch_status": true }, "style": "powerline", "template": " {{ .HEAD }}{{ if .Staging.Changed }}<#FF6F00> \uf046 {{ .Staging.String }}</>{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ", "type": "git" }, { "background": "#575656", "foreground": "#d6deeb", "properties": { "style": "roundrock", "threshold": 0 }, "style": "diamond", "template": " {{ .FormattedMs }}", "trailing_diamond": "\ue0b4", "type": "executiontime" } ], "type": "prompt" }, { "alignment": "right", "overflow": "break", "segments": [ { "background": "#d6deeb", "foreground": "#011627", "leading_diamond": "\ue0b6", "style": "diamond", "template": "\uf489 {{ .Name }} ", "type": "shell" }, { "background": "#234d70", "foreground": "#d6deeb", "properties": { "time_format": "15:04:05" }, "style": "diamond", "template": " \ue641 {{ .CurrentDate | date .Format }}", "trailing_diamond": "\ue0b4", "type": "time" }, { "background": "#306998", "foreground": "#FFE873", "leading_diamond": "\ue0b2", "style": "diamond", "template": "\ue235 {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}{{ end }}", "trailing_diamond": "<transparent,#306998>\ue0b2</>", "type": "python" } ], "type": "prompt" }, { "alignment": "left", "newline": true, "segments": [ { "foreground": "#22da6e", "foreground_templates": ["{{ if gt .Code 0 }}#ef5350{{ end }}"], "properties": { "always_enabled": true }, "style": "plain", "template": "\ue285\ue285", "type": "status" } ], "type": "prompt" } ], "final_space": true, "version": 2 }
复现步骤
- 打开PowerShell,创建文件夹并执行
git init - 创建文件(如
New-Item bar.txt),可观察Git Powerline块从绿色变为黄色并出现问号 - 需安装JetBrainsMono Nerd Font或CascaydiaCove Nerd Font
- 确保
Microsoft.PowerShell_profile.ps1中包含以下内容:
Import-Module posh-git oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/theme-name.omp.json" | Invoke-Expression
解决方案
问题根源
问号是因为Git段模板中使用的{{ .Working.String }}会输出Git原生的状态符号(比如??代表未追踪文件),这些符号没有被替换为Nerd Font可渲染的图标,导致终端显示问号。
修复步骤
修改Git段的template字段,替换原生符号为图标化展示,推荐两种方式:
方式1:手动替换原生符号
将Git段的template改为:
"template": " {{ .HEAD }}{{ if .Staging.Changed }}<#FF6F00> \uf046 {{ .Staging.String }}</>{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Working.Changed }} \uf044 {{ replace .Working.String '??' '\uf059' }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ",
用replace函数把Git原生的??符号替换为Nerd Font的问号图标\uf059,确保字体能正确渲染。
方式2:使用内置状态图标(推荐)
直接使用Oh My Posh内置的{{ .Working.Icon }}和{{ .Working.Count }}变量,自动适配图标:
"template": " {{ .HEAD }}{{ if .Staging.Changed }}<#FF6F00> \uf046 {{ .Staging.Count }}</>{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.Icon }} {{ .Working.Count }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ",
这种方式会根据文件状态(新增、修改、删除)自动显示对应图标,无需手动替换符号。
额外检查
- 确认终端已设置JetBrainsMono Nerd Font为默认字体,重启终端生效
- 执行
oh-my-posh upgrade更新到最新版本,确保功能兼容 - 删除
Microsoft.PowerShell_profile.ps1中的Import-Module posh-git行,Oh My Posh已内置Git状态获取功能,两者可能冲突
内容的提问来源于stack exchange,提问作者Felix
相关产品推荐
相关产品推荐

