小小滴我 發表於 2026-1-20 16:22:00

fedora 下使用 oh-my-posh 美化 bash

<h1 id="fedora-下使用-oh-my-posh-美化-bash">fedora 下使用 oh-my-posh 美化 bash</h1>
<h2 id="一为什么选择-oh-my-posh">一、为什么选择 oh-my-posh</h2>
<p>oh-my-posh 有着跨平台统一主题,Windows PowerShell 和 Linux Bash 用同一套 <code>.omp.json</code> 配置。而且配置简单,一个 JSON 文件搞定一切。</p>
<h2 id="二安装-oh-my-posh">二、安装 oh-my-posh</h2>
<pre><code class="language-bash">sudo dnf install oh-my-posh
</code></pre>
<p>检查是否安装成功:</p>
<pre><code class="language-bash"># 查看版本
oh-my-posh --version
# 示例输出为
28.10.0
</code></pre>
<p>完成安装。</p>
<h2 id="三安装-nerd-fonts">三、安装 Nerd Fonts</h2>
<p>oh-my-posh 的箭头、Git 图标等依赖 Nerd Fonts。可以用 oh-my-posh 内置命令安装:</p>
<pre><code class="language-bash">oh-my-posh font install Meslo
</code></pre>
<p>它会自动下载并安装 MesloLGM Nerd Font(最兼容的主题字体)。</p>
<p>然后在终端上设置字体即可。</p>
<h2 id="四配置-oh-my-posh-到-bash">四、配置 oh-my-posh 到 Bash</h2>
<p>编辑 bash 配置文件:</p>
<pre><code class="language-bash">nano ~/.bashrc
</code></pre>
<p>在文件末尾添加:</p>
<pre><code class="language-bash"># 启用 oh-my-posh(使用默认主题)
eval "$(oh-my-posh init bash)"
</code></pre>
<p>保存退出后,重载配置:</p>
<pre><code class="language-bash">source ~/.bashrc
</code></pre>
<h2 id="五使用自定义主题">五、使用自定义主题</h2>
<pre><code class="language-bash"># 创建主题目录
mkdir -p ~/.config/oh-my-posh/themes
# 进入主题目录
cd ~/.config/oh-my-posh/themes
</code></pre>
<p>在官网(oh-my-posh themes)上选择自己喜欢的主题并下载:</p>
<pre><code class="language-bash"># 以 powerlevel10k_rainbow 主题为例
wget https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/powerlevel10k_rainbow.omp.json
</code></pre>
<p>修改 <code>~/.bashrc</code> 使用自定义主题:</p>
<pre><code class="language-bash">eval "$(oh-my-posh init bash --config ~/.config/oh-my-posh/themes/powerlevel10k_rainbow.omp.json)"
</code></pre>
<p>重载配置即可:</p>
<pre><code class="language-bash">source ~/.bashrc
</code></pre>
<h2 id="六修改主题使-conda-环境可见">六、修改主题使 conda 环境可见</h2>
<p>以 <code>powerlevel10k_rainbow</code> 这一主题为例,当激活 conda 环境之后 bash 并不会显示环境。所以需要对该主题的 <code>.json</code> 文件做一定修改以适配 conda 的显示。</p>
<p>首先复制一份文件到 <code>powerlevel10k_rainbow_with-conda.omp.json</code> 再进行修改,避免污染原文件:</p>
<pre><code class="language-bash">cp ~/.config/oh-my-posh/themes/powerlevel10k_rainbow.omp.json ~/.config/oh-my-posh/themes/powerlevel10k_rainbow_with-conda.omp.json
</code></pre>
<p>修改:</p>
<pre><code class="language-bash">nano .config/oh-my-posh/themes/powerlevel10k_rainbow_with-conda.omp.json
</code></pre>
<p>找到 python 的 segment :</p>
<p><img src="https://img2024.cnblogs.com/blog/3078251/202601/3078251-20260120162205568-1380860983.png" alt="segment 替换" loading="lazy"></p>
<p>将这一块替换为下面的段落(也可以按照自己需求编写):</p>
<pre><code class="language-bash">        {
        "type": "python",
        "style": "powerline",
        "powerline_symbol": "\ue0b2",
        "foreground": "#111111",
        "background": "#FFDE57",
        "invert_powerline": true,
        "properties": {
                "display_mode": "environment",
                "fetch_virtual_env": true,
                "display_version": true,
                "home_enabled": true,
                "prefix": " \ue235 ",
                "postfix": ""
        },
        "template": "{{ if .Venv }}\ue73f{{ .Venv }}{{ else if .Version }}\ue73f{{ .Version }}{{ end }}"
        },
</code></pre>
<p>编辑 <code>~/.bashrc</code>, 将 <code>eval "$(oh-my-posh init bash --config ~/.config/oh-my-posh/themes/powerlevel10k_rainbow.omp.json)"</code> 修改为:</p>
<pre><code class="language-bash">eval "$(oh-my-posh init bash --config ~/.config/oh-my-posh/themes/powerlevel10k_rainbow_with-conda.omp.json)"
</code></pre>
<p>重载配置即可:</p>
<pre><code class="language-bash">source ~/.bashrc
</code></pre>
<p>效果:</p>
<p><img src="https://img2024.cnblogs.com/blog/3078251/202601/3078251-20260120162206058-1852240712.png" alt="效果展示" loading="lazy"></p>
<hr>
<blockquote>
<p><strong>版权声明:</strong> 本文为博主「张火火isgudi」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。<br>
<strong>原文链接:</strong> https://www.cnblogs.com/zbyisgudi/p/19507440</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/zbyisgudi/p/19507440
頁: [1]
查看完整版本: fedora 下使用 oh-my-posh 美化 bash