在 Linux 或 macOS 上,很多开发者喜欢使用 zsh + oh-my-zsh 来获得强大的命令补全、历史搜索和美化提示。在 Windows 上,我们也可以打造类似体验,利用 PowerShell 7 + Oh My Posh + PSReadLine + posh-git,并让 VS Code 终端完美适配。本文将详细介绍安装与配置步骤。
所有相关源码示例、流程图、模型配置与知识库构建技巧,我也将持续更新在Github:AIHub,欢迎关注收藏!
Oh My Posh 使用了大量图标和 Git 状态符号,如果字体不支持就会出现乱码。推荐字体包括:
下载地址:Nerd Fonts 官网

安装步骤:

PowerShell 7 是新版跨平台 PowerShell,提供更多特性和兼容性。
官方安装指南:Installing PowerShell on Windows

安装完成后,可以在命令行确认版本:
pwsh --version如果显示
7.x.x,说明安装成功。
注意Windows 默认仍保留 PowerShell 5.1,VS Code 或 CMD 可能默认启动的是旧版,需要手动设置。
安装完之后到设置如果看不到PowerShell 7,那么手动添加一个配置文件


在 PowerShell 7 中执行:
# 更新 PowerShellGet
Install-Module -Name PowerShellGet -Force
# 安装 Oh My Posh
winget install JanDeDobbeleer.OhMyPosh -s winget
# 安装 posh-git(Git 自动补全)
Install-Module posh-git -Scope CurrentUser -Force
# 安装 PSReadLine(增强自动补全)
Install-Module PSReadLinePowerShell 配置文件路径:
$PROFILE可以用 VS Code 打开:
code $PROFILE在文件末尾加入:
# Tab 键显示自动补全菜单
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
# 上下方向键搜索历史命令
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
# 启动 Oh My Posh
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/jandedobbeleer.omp.json" | Invoke-Expression
# 启用 Git 自动补全
Import-Module posh-git保存并重启 PowerShell,就可以啦。

为了在 VS Code 内部也能使用 PowerShell 7 和 Oh My Posh,需要修改 VS Code 的 settings.json。
Ctrl + Shift + Psettings json
或者点击右上角的按钮转换成json

{
"terminal.integrated.defaultProfile.windows": "PowerShell 7",
"terminal.integrated.profiles.windows": {
"PowerShell 7": {
"path": "D:\\Program Files (x86)\\PowerShell\\7\\pwsh.exe",
"icon": "terminal-powershell"
}
},
"terminal.integrated.fontFamily": "JetBrainsMono Nerd Font"
}注意:路径根据实际安装位置调整。 字体名称必须和安装的 Nerd Font 对应。

大功告成!
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。