Skip to Content
OSWindows使用 oh-my-posh 优化 Windows Terminal 体验

使用 oh-my-posh 优化 Windows Terminal 体验

升级 Powershell 版本(可选)

官方升级文档 

如果使用 Windows Terminal,升级后在 Windows Terminal 的设置里面把默认的 shell 改成升级后的 Powershell。

安装 oh-my-posh

如果有杀毒软件误报毒的情况可以看看下面官方文档怎么解决的。

https://ohmyposh.dev/docs/installation/windows 

# 安装 oh-my-posh winget install JanDeDobbeleer.OhMyPosh -s winget
# 更新 oh-my-posh winget upgrade JanDeDobbeleer.OhMyPosh -s winget

配置 oh-my-posh

配置字体 fonts

这里我按官方给的推荐字体来安装了。

https://ohmyposh.dev/docs/installation/fonts 

oh-my-posh font install meslo

在 Terminal 配置文件里面设置字体,打开 Windows Terminal 的设置,找到 profiles -> defaults -> font -> face,把它改成下面的字体

{ "profiles": { "defaults": { "font": { "face": "MesloLGM Nerd Font" } } } }

配置 vscode 的 json 文件,添加字体配置

"terminal.integrated.fontFamily": "MesloLGM Nerd Font"

配置提示 prompt

因为我 WT 用的是 powershell,所以这里只给出了 powershell 的配置,其他的看下面官方文档

https://ohmyposh.dev/docs/installation/prompt 

# 使用记事本打开 powershell 的配置文件 # if the profile file does not exist, create it if (-not (Test-Path $profile)) { New-Item $profile -Force } # Open the profile file in notepad notepad $PROFILE

在配置文件内添加一行,让 powershell 每次打开都自动配置 oh-my-posh

oh-my-posh init pwsh | Invoke-Expression

配置主题 themes

可以在这里 看到所有的主题

我喜欢和 Linux / Mac 上保持一致,还是用的 powerlevel10k 的主题,这里安装也需要放到 powershell 的配置文件内,每次都运行

notepad $PROFILE oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/powerlevel10k_rainbow.omp.json" | Invoke-Expression
# 如果安装主题的时候报了下面的错误,可以运行下面的命令 # Get-PSReadLineKeyHandler : A positional parameter cannot be found that accepts argument 'Spacebar'. Install-Module PsReadLine -Force # 然后重新打开一遍 Windows Terminal,再装一遍主题

优化 powershell 启动

以管理员身份运行 powershell,运行下面的命令, 来源 

$env:PATH = [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory() [AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { $path = $_.Location if ($path) { $name = Split-Path $path -Leaf Write-Host -ForegroundColor Yellow "`r`nRunning ngen.exe on '$name'" ngen.exe install $path /nologo } }

其他配置

模块

# 类似 autojump 的功能,快速跳转到历史目录 Install-Module -Name ZLocation # ReadLine 命令自动补全 Install-Module PSReadLine -Force

自用别名 alias

# ============================================================ # Aliases # ============================================================ $aliases = [ordered]@{ # Scoop s = 'scoop' # Git g = 'git' gs = 'git stash' gc = 'git commit' gr = 'git reset --soft HEAD~1' gaa = 'git add .' gch = 'git checkout' # ZLocation -> autojump j = 'z' # Editor c = 'code .' # Bun br = 'bun run' bx = 'bunx' # Python python = 'uv run python' pip = 'uv pip' py = 'python' # System ll = 'Get-ChildItem -Force' la = 'Get-ChildItem -Force -Hidden' # Tools ard = 'aria2c --summary-interval=10 -x 3 --allow-overwrite=true -Z' } foreach ($alias in $aliases.GetEnumerator()) { Set-Item -LiteralPath "Function:global:$($alias.Key)" -Value "& $($alias.Value) @args" } Remove-Variable aliases
Last updated on