前言

工具简介

Oh My Posh 是一款用于美化 Windows 终端和 PowerShell 的工具,支持:

  • 自定义提示符和主题
  • 增强历史记录功能
  • 个性化视觉效果

oh-my-posh官方文档

安装配置流程

一、Oh My Posh 安装

核心组件安装

1
2
3
winget install JanDeDobbeleer.OhMyPosh -s winget

Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1'))

配置文件设置

(1) 检查配置文件

1
Test-path $profile

(2) 创建配置文件(不存在就创建文件)

1
notepad $profile

(3) 在文件里输入内容,后关闭PowerShell

1
2
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History

保存文件后会在目录:C:\Users\用户\Documents\WindowsPowerShell,下生成Microsoft.PowerShell_profile.ps1文件

常见报错处理

Set-PSReadLineOption : 找不到与参数名称“PredictionSource”匹配的参数

  1. 确保你的 PSReadLine 模块是最新版本。你可以通过以下命令更新 PSReadLine 模块
    1
    Install-Module -Name PSReadLine -Force
  2. 上面安装完成后还是报错,检查 PowerShell 版本
    如果版本低于 7.2,你需要升级你的 PowerShell。你可以从 PowerShell GitHub 页面下载最新版本。
    1
    $PSVersionTable.PSVersion

二、个性化设置

1. 字体安装(可选)

操作步骤:

1
oh-my-posh font install

终端设置(MesloLGM字体)以普通身份再运行 PowerShell :

CTRL + SHIFT + ,
打开一个settings.json,找到profiles->defaults设置对应字体,保存退出

1
2
3
4
5
6
7
8
9
10
11
12
{
"profiles":
{
"defaults":
{
"font":
{
"face": "MesloLGM Nerd Font"
}
}
}
}

主题配置

(1) 查看可用主题,主题预览

1
start $env:POSH_THEMES_PATH

(2) 应用主题配置,打开Microsoft.PowerShell_profile.ps1文件

1
notepad $profile

添加主题,这里选择的是bubblesextra.omp.json主题,个人喜好自己修改

1
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\bubblesextra.omp.json" | Invoke-Expression

到这里 PowerShell 的已设置好了,但是CMD并没有这个效果,继续折腾


Terminal-Icons项目地址(非必须)

Terminal-Icons 是一个 PowerShell 模块,它可以让你的终端显示带有图标的文件和目录列表。该项目基于Nerd Fonts提供的定制字体,这些字体包含了大量自定义的图形和图标。通过这个模块,你可以使终端看起来更专业,更具个性化。

先执行,否则执行后面命令会报错

1
Set-PSRepository -Name PSGallery -InstallationPolicy trusted

终端使用管理员身份打开 PowerShell

1
Install-Module -Name Terminal-Icons -Repository PSGallery

打开Microsoft.PowerShell_profile.ps1文件

1
notepad $profile

添加Icons,保存

1
Import-Module -Name Terminal-Icons

Terminal-Icons 让终端不再是单调的文字展示,而是转变为视觉上的盛宴。如果你是一个热爱命令行界面的人,那么这个项目绝对值得你尝试。现在就去安装它,给你的终端增添一份色彩吧!


clink安装

clink 是一款增强 CMD 功能的小工具,支持自动补全、命令历史等功能,极大提升了 CMD 的使用体验。

包管理工具安装

1
2
3
winget install clink #winget安装
或者
scoop install clink #scoop安装

手动安装

clink官方网站下载.zip安装文件或者.setup.exe安装文件(推荐前者)

安装注意事项

如果选择的是.exe安装,记得勾选Autorun when cmd.exe startsUse enhanced default settings,前者会帮你自动将clinks插入到cmd中,而后者可以帮你进行一些默认的配置,避免自己配置的时候有一些问题。

1
clink autorun install -- --quiet

启用自动补全

1
clink set autosuggest.enable true

配置 oh-my-posh 样式

创建 Lua 配置文件
在 C:\Users\<用户名>\AppData\Local\clink 目录下,新建一个名为 oh-my-posh.lua 的文件。

加载 oh-my-posh 主题
在文件中写入以下代码,以将 oh-my-posh 的样式应用到 CMD:

1
2
3
4
5
6
7
8
local themes_path = os.getenv("POSH_THEMES_PATH")
if themes_path then
local config_path = themes_path:gsub("\\", "/") .. "/bubblesextra.omp.json"
local command = string.format('oh-my-posh init cmd --config "%s"', config_path)
load(io.popen(command):read("*a"))()
else
print("环境变量 POSH_THEMES_PATH 未设置")
end

bubblesextra.omp.json是主题,可以自行更换
配置完成后,重新打开 CMD,即可看到应用了 oh-my-posh 的自定义提示符样式。