自定义
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
标准初始化会设置 Oh My Posh 默认的内置主题。
要设置新配置或主题,您需要修改 profile 或 .<shell>rc 脚本中 oh-my-posh init <shell> 命令的 --config 选项(详见提示)。
--config 参数支持以下三种取值方式:
-
本地配置文件路径
--config 'C:/Users/Posh/myconfig.omp.json' -
主题名称(不带扩展名)
--config 'jandedobbeleer' -
指向远程配置的 URL
--config 'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/jandedobbeleer.omp.json'
使用主题名称(如 jandedobbeleer)或远程 URL 需要设备处于联网状态,系统会在 shell 启动时下载配置。虽然已内置缓存机制,但为获得最佳性能,建议使用本地配置文件。
设置配置
以下示例使用了 jandedobbeleer 主题的本地路径,请根据您的配置文件位置(本地或远程)调整 --config 参数的值。
- bash
- cmd
- elvish
- fish
- nu
- powershell
- xonsh
- zsh
Use the full path to the config file, not the relative path or ~ as a shorthand for $HOME.
Adjust the Oh My Posh init line in ~/.bashrc (could be ~/.profile or ~/.bash_profile depending
on your environment) by adding the --config flag with the location of your configuration.
eval "$(oh-my-posh init bash --config ~/jandedobbeleer.omp.json)"
Once altered, reload your profile for the changes to take effect.
exec bash
Adjust the Oh My Posh init line in oh-my-posh.lua by adding the --config flag with the location
of your configuration.
load(io.popen('oh-my-posh init cmd --config C:/Users/Posh/jandedobbeleer.omp.json'):read("*a"))()
Use the full path to the config file, not the relative path. You can make use of forward slashes to avoid having to use double backslashes.
Once altered, restart cmd for the changes to take effect.
Adjust the Oh My Posh init line in ~/.elvish/rc.elv by adding the --config flag with the location
of your configuration.
eval (oh-my-posh init elvish --config ~/jandedobbeleer.omp.json)
Once added, reload your profile for the changes to take effect.
exec elvish
It is recommended to use the latest version of Fish. Versions below 4.1.0 have issues and do not support [transient prompt].
Adjust the Oh My Posh init line in ~/.config/fish/config.fish by adding the --config flag with
the location of your configuration.
oh-my-posh init fish --config ~/jandedobbeleer.omp.json | source
Once altered, reload your config for the changes to take effect.
. ~/.config/fish/config.fish
Oh My Posh requires Nushell v0.104.0 or higher.
Adjust the Oh My Posh init line in the Nushell config file ($nu.config-path) by adding the --config flag
with the location of your configuration.
oh-my-posh init nu --config ~/jandedobbeleer.omp.json
Adjust the Oh My Posh init line in your $PROFILE by adding the --config flag with the location
of your configuration.
oh-my-posh init pwsh --config ~/jandedobbeleer.omp.json | Invoke-Expression
Once altered, reload your profile for the changes to take effect.
. $PROFILE
When the above command gives an error, make sure to create the profile first and add the oh-my-posh init above.
New-Item -Path $PROFILE -Type File -Force
In this scenario, it can also be that PowerShell blocks running local scripts. To solve that, set PowerShell
to only require remote scripts to be signed using Set-ExecutionPolicy RemoteSigned, or sign the profile.
Adjust the Oh My Posh init line in ~/.xonshrc by adding the --config flag with the location of your configuration.
execx($(oh-my-posh init xonsh --config ~/jandedobbeleer.omp.json))
Once added, reload your profile for the changes to take effect.
exec xonsh
Adjust the Oh My Posh init line in ~/.zshrc by adding the --config flag with the location of your configuration.
eval "$(oh-my-posh init zsh --config ~/jandedobbeleer.omp.json)"
Once altered, reload your profile for the changes to take effect.
exec zsh
在 Windows 和 WSL 中使用 oh-my-posh 时,您可以通过指向 Windows 用户主目录中的主题来与 WSL 共享主题。
在 WSL 中,可通过以下路径访问 Windows 用户主目录:/mnt/c/Users/<WINDOWSUSERNAME>。
自定义配置
您可能喜欢某个主题但不喜欢其配色,或者想调整/添加某个组件(segment),又或是替换某些图标。无论何种情况,请先阅读可用选项,建议从配置部分开始。
您可以将当前主题(默认主题或通过 --config 设置的主题)导出为喜欢的格式(json、yaml 或 toml),用于调整并保存为自定义配置。
oh-my-posh config export --config jandedobbeleer --output ~/.mytheme.omp.json
实时重载
出于性能考虑,配置信息默认会被缓存。若您修改了配置文件后希望在不重启 shell 的情况下实时更新提示符,可使用以下命令启用实时重载功能。
oh-my-posh enable reload
您可通过以下命令禁用实时重载:
oh-my-posh disable reload
预览更改
如需预览修改效果,可使用以下命令渲染所有配置的提示符:
oh-my-posh print preview
若需强制渲染所有组件(无论当前上下文如何),请使用 --force 标志。
oh-my-posh print preview --force
阅读文档
要深入掌握主题定制方法,请详细阅读配置和组件部分的文档。配置部分涵盖 Oh My Posh 主题的核心结构和概念,而组件部分则详解每个可用组件的配置方法。