windows通过oh-my-posh终端美化

914 阅读1分钟

微软商店下载powershell7和terminal

terminal.png

powershell.png

下载安装oh-my-posh推荐字体

  1. 进入oh-my-posh主页,点击左侧导航栏字体一项。 2.在右边找到推荐字体地址并点击跳转,跳转后下载。 3.字体是一个压缩包点击解压缩然后选中全部字体为所有用户安装。

font-1.png

font-2.png

配置powershell以及terminal

  1. 找到terminal启动文件右键菜单栏选择以管理员方式运行,进入terminal设置页面,选中左边导航栏启动项,配置默认配置文件为powershell。

  2. 再选中左侧配置文件下方选择powershell,点击右边外观,选择一个我们下载的字体。 注意选择字体是为了不乱码这一步是必须的

terminal-1.png

terminal-2.png

terminal-3.png

powershell-1.png

powershell-2.png

通过powershell安装配置oh-my-posh

下载安装oh-my-posh

image-20241009193351311.png

输入oh-my-posh get shell查看当前终端

image-20241009193752436.png

运行以下代码后就能看到效果了

& ([ScriptBlock]::Create((oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" --print) -join "`n"))

运行Get-PoshThemes查看所有主题(官方已删除此命令建议直接到官方查询主题样式

image-20241009194358884.png

image-20241009194513162.png

输出值最后一行是更改主题代码,只需要更换红色方框标注的主题名字就好

oh-my-posh init pwsh --config 'C:\Users\Administrator\AppData\Local\Programs\oh-my-posh\themes\此处选择你的主题名字.omp.json' | Invoke-Expression

为了防止每次都手动启动oh-my-posh,我们设置一个自启动文件

image-20241009194915884.png

如果显示没有此文件可以先创建一个运行以下命令然后再编辑

New-Item -Path $PROFILE -Type File -Force

在文件中写入

# ======================== # Oh My Posh 初始化 # ======================== 
if (Get-Command oh-my-posh -ErrorAction SilentlyContinue) { 
# 使用官方环境变量,避免硬编码路径 
$themePath = Join-Path $env:POSH_THEMES_PATH "clean-detailed.omp.json" 
if (Test-Path $themePath) { 
oh-my-posh init pwsh --config $themePath | Invoke-Expression 
} else { Write-Host "⚠️ 主题文件未找到: $themePath" -ForegroundColor Yellow 
# 回退到默认主题 
oh-my-posh init pwsh | Invoke-Expression 
} } else { 
Write-Host "ℹ️ Oh My Posh 未安装,跳过初始化" -ForegroundColor Gray 
} 
# ======================== # Terminal-Icons(可选) # ======================== 
if (Get-Module -ListAvailable -Name Terminal-Icons) { 
Import-Module Terminal-Icons } else { 
Write-Host "💡 提示: 安装 Terminal-Icons 可获得彩色文件图标" 
-ForegroundColor Cyan Write-Host " 运行: Install-Module -Name Terminal-Icons -Repository PSGallery" -ForegroundColor Cyan }

image-20241009195236224.png

保存退出编辑,然后运行,运行结束关闭terminal再打开就能自动显示了

. $PROFILE

下载 terminal-icons

下载 terminal-icons这样文件图标就能正常显示了

Install-Module -Name Terminal-Icons -Repository PSGallery

image-20241009195737185.png

最后解决vscode会乱码

使用 Visual Studio Code 时,您需要配置集成终端以使用 Nerd 字体。这可以通过更改Integrated: Font Family终端设置中的值来完成(默认快捷方式:CTRL + ,并搜索Integrated: Font Family或通过Users-> Features-> Terminal)。

如果您使用基于 JSON 的设置,则需要更新该值。以Nerd Fontterminal.integrated.fontFamily为例:MesloLGM Nerd Font

image-20241009200619626.png

视频教程地址