常见问题解答
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
在验证任何问题前,请确保你使用的是 Oh My Posh 的最新版本,并且终端和 shell 都已更新至最新。
升级到 PowerShell 7.4 后提示符显示异常
PowerShell 7.4 存在编码问题,这会影响 Oh My Posh 的渲染效果。 你可以通过强制整个 shell 使用 UTF8 编码来临时解决此问题。
[Console]::OutputEncoding = [Text.Encoding]::UTF8
oh-my-posh init pwsh --config ~/custom.omp.json | Invoke-Expression
强制整个会话使用 UTF8 可能对其他可执行程序产生意外影响。我们对此不承担责任, 请将此视为临时解决方案,直到 PowerShell 的原始问题修复。
如何禁用更新通知
你可以通过以下命令一次性禁用更新通知:
oh-my-posh disable notice
如需重新启用,请运行:
oh-my-posh enable notice
提示符响应缓慢(命令间显示延迟)
出现的延迟可能与杀毒软件的实时防护功能有关。
为减轻延迟,请确保 Windows Defender 或你的主杀毒软件已为可执行文件完整路径设置排除项。要获取可执行文件完整路径,可在 PowerShell 中运行:
(Get-Command oh-my-posh).Source
此外请检查杀毒软件设置中是否启用了脚本执行时扫描功能。即使设置了排除项,某些杀毒软件仍会主动扫描脚本,从而造成明显延迟。
请始终谨慎操作以平衡系统性能与安全性。修改安全设置或添加排除项可能使系统面临潜在威胁。
- powershell
- others
You can use the oh-my-posh debug functionality to see where Oh My Posh spends its time.
In case there's no clear culprit (timings indicate everything's OK), chances are some modules are the culprit.
We bootstrap a few PowerShell modules to maximize compatibility, but sometimes these can introduce unwanted side-effects.
The modules we support are:
- posh-git
You can use Oh My Posh's built-in debug functionality to identify slow segments.
oh-my-posh debug
Whenever there's a segment that spikes, see if there might be updates to the underlying functionality (usually shell commands).
若仅 Git 仓库路径操作缓慢,可尝试运行 git gc 清理并优化本地仓库。
如果所有方法均无效,欢迎新建 issue。
提示符中图标显示为方框
当前使用的字体缺少必要的扩展字形集(如 Nerd Font 提供的字形)。
Windows Terminal 默认搭载的 Cascadia Code 虽提供 powerline 变体 Cascadia Code PL,
但仍缺失部分图标。你可回退到带 .minimal 后缀的主题,或改用 Nerd Font。
若确认环境配置正确,请参阅字体章节获取更多背景信息。
文本修饰样式(如粗体/暗色)无效
文本修饰样式取决于终端模拟器的支持能力。
在 Linux 系统中,可通过查询 terminfo 数据库快速检查终端是否支持特定样式转义序列。
参考 ArchWiki 的相关页面。
Windows 用户建议使用 Windows Terminal,它高度兼容 xterm-256color 特性。
详见 PowerShell 终端支持文档及此 GitHub 评论。
Windows Terminal:段/文本间出现意外空格
Windows Terminal 在渲染特定字形时存在问题,相关改进已在待办列表。
临时解决方案是在末尾添加不可见字符 (\u2800),或在图标前插入零宽字符 (\u200a)。
若尚未导出配置(使用默认配置或通过 --config 标志使用预设主题),可按照此处指引导出到本地文件,
以便调整段的 template 属性。
在以下示例中,假设执行段图标 \ueba2 在 Windows Terminal 中显示后出现意外空格。
- json
- yaml
- toml
{
"type": "executiontime",
"template": ""
}
type: executiontime
template:
type = "executiontime"
template = ""
通过在图标后立即添加 \u2800 进行调整。
- json
- yaml
- toml
{
"type": "executiontime",
"template": "⠀"
}
type: executiontime
template: ⠀
type = "executiontime"
template = "⠀"
Jetbrains 终端:图标无法渲染
其终端需要改进支持,目前仅支持 UTF-8 而不支持 UTF-16。 相关问题可在此处跟进。
退出 VIM 或使用 PowerShell 进度引导后颜色异常
此问题由 Windows Terminal 和/或 VIM 引起,相关追踪:Windows Terminal 和 VIM。
Conda:环境名称显示在提示符前
Conda 会自动在当前提示符前添加环境名称。 若希望仅通过 Oh My Posh 设置提示符,可通过以下配置隐藏 Conda 的提示。 请确保在初始化 Oh My Posh 前添加此设置。
conda config --set changeps1 False
Python venv:激活虚拟环境时提示符变化
使用 venv 创建的虚拟环境会自动在提示符中添加活动环境名称。
要禁用此行为,在系统中设置环境变量 VIRTUAL_ENV_DISABLE_PROMPT 为 1。示例文件:
注:路径中的波浪号 (~) 指向用户主目录。
- powershell
- fish
- others
# Your PowerShell $PROFILE
$env:VIRTUAL_ENV_DISABLE_PROMPT=1
# ~/.config/fish/config.fish
set -x VIRTUAL_ENV_DISABLE_PROMPT 1
# ~/.bashrc (bash) or ~/.zprofile or ~/.zshrc (zsh)
export VIRTUAL_ENV_DISABLE_PROMPT=1
PowerShell:无法将 "Set-Theme" 识别为 cmdlet、函数、脚本文件或可运行程序
请按此迁移指南操作。
PowerShell:运行在受限语言模式下
出现此提示是因为看到以下消息:
[WARNING] ConstrainedLanguage mode detected, unable to set console to UTF-8.
When using PowerShell in ConstrainedLanguage mode, please set the
console mode manually to UTF-8. See here for more information:
https://ohmyposh.dev/docs/faq#powershell-running-in-constrainedlanguage-mode
在 ConstrainedLanguage 模式下运行时,无法将控制台设为 UTF-8,这将导致提示符渲染异常。 Windows 系统可通过几种方案在操作系统层设置 UTF-8,其他系统不受影响。
手动配置后,可通过在导入 Oh My Posh 前向 $PROFILE 添加以下内容消除提示:
$env:POSH_CONSTRAINED_LANGUAGE = 1
PowerShell:无法将 ".../oh-my-posh.exe" 识别为 cmdlet 名称...
例如:
&: The term 'C:/Users/TommiGr├╢nlund/.oh-my-posh/oh-my-posh.exe' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
此问题源于 Windows PowerShell 尚未默认使用 UTF-8。可通过在初始化 Oh My Posh 时设置 UTF-8 解决。
如果路径包含非 ASCII 字符,非 UTF-8 环境的 PowerShell 可能提供错误路径,导致初始化失败。
非 ASCII 路径的典型场景:
- 计算机用户名包含非 ASCII 字符
- 配置文件位于
$HOME目录下
请编辑 $PROFILE 文件,找到初始化 Oh My Posh 的行(如下所示),修改为:
$previousOutputEncoding = [Console]::OutputEncoding
[Console]::OutputEncoding = [Text.Encoding]::UTF8
try {
oh-my-posh init pwsh --config ~/custom.omp.json | Invoke-Expression
} finally {
[Console]::OutputEncoding = $previousOutputEncoding
}
或使整个 Shell 启用 UTF-8(注意:可能有副作用)。在 $PROFILE 顶部添加:
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
Zsh:无命令历史记录(Ctrl+R 无效)
此问题发生在使用原生 zsh 搭配 Oh My Posh 时。
可通过在 ~/.zshrc 中添加正确配置修复:
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt appendhistory
ZSH:无此 shell 函数 'zle-line-init'
通常由 .zshrc 中存在两条 Oh My Posh 初始化命令引起,删除其中一条。详见此处。
Fish:显示当前绑定(Vim)模式
默认情况下,Oh My Posh 不会重新渲染提示符(即生成新提示符),直到运行新命令为止。因此,你需要将 $fish_bind_mode 导出到 FISH__BIND_MODE,并在 $fish_bind_mode 变化时调用 omp_repaint_prompt 函数来重新渲染提示符:
function rerender_on_bind_mode_change --on-variable fish_bind_mode
if test "$fish_bind_mode" != paste -a "$fish_bind_mode" != "$FISH__BIND_MODE"
set -gx FISH__BIND_MODE $fish_bind_mode
omp_repaint_prompt
end
end
接下来,覆盖 fish_default_mode_prompt 函数以防止其回显当前模式:
function fish_default_mode_prompt --description "Display vi prompt mode"
# This function is masked and does nothing
end
之后,你可以在模板中使用该值。以下代码复制了 Fish 文档中的示例:
- json
- yaml
- toml
{
"type": "text",
"template": "{{ if eq .Env.FISH__BIND_MODE \"default\" }}<red>[N]</>{{ else if eq .Env.FISH__BIND_MODE \"insert\" }}<green>[I]</>{{ else if eq .Env.FISH__BIND_MODE \"replace_one\" }}<green>[R]</>{{ else if eq .Env.FISH__BIND_MODE \"visual\"}}<brmagenta>[V]</>{{ else }}<red>[?]</>{{ end }}"
}
type: text
template: '{{ if eq .Env.FISH__BIND_MODE "default" }}<red>[N]</>{{ else if eq
.Env.FISH__BIND_MODE "insert" }}<green>[I]</>{{ else if eq
.Env.FISH__BIND_MODE "replace_one" }}<green>[R]</>{{ else if eq
.Env.FISH__BIND_MODE "visual"}}<brmagenta>[V]</>{{ else }}<red>[?]</>{{ end
}}'
type = "text"
template = "{{ if eq .Env.FISH__BIND_MODE \"default\" }}<red>[N]</>{{ else if eq .Env.FISH__BIND_MODE \"insert\" }}<green>[I]</>{{ else if eq .Env.FISH__BIND_MODE \"replace_one\" }}<green>[R]</>{{ else if eq .Env.FISH__BIND_MODE \"visual\"}}<brmagenta>[V]</>{{ else }}<red>[?]</>{{ end }}"
Fish:整合 Alt+← 和 Alt+→ 绑定到 prevd 和 nextd
Fish 支持使用 Alt+← (Alt+左方向键) 和 Alt+→ (Alt+右方向键) 绑定来浏览目录历史记录。
要让 oh-my-posh 在浏览目录历史时更新目录上下文,请使用在 $PWD 变量变化时调用 omp_repaint_prompt 的函数:
function rerender_on_dir_change --on-variable PWD
omp_repaint_prompt
end
更新 Nerd Font 后提示符显示未知字符
Nerd Fonts 在 v3 版本中将图标移到了字体中的不同位置。这可能导致提示符显示未知字符。Oh My Posh 内置了迁移功能来解决此问题。
要执行迁移,请运行以下命令:
oh-my-posh config migrate glyphs --write
这将更新你的配置文件以使用新的字形位置。请注意图标本身也可能有所更新,因此显示效果可能不同。当前配置的备份文件可在相同路径找到,扩展名为 .bak。
Xonsh:右侧提示符跳至屏幕底部
这是 Xonsh 的已知问题,相关跟踪记录见此处。