起因
这次是因为想着没事找事干 来做一个终端美化的东西,并且扩展像 Linux 终端一样的自动补全和命令提示,于是有了本次的行动经典没事找事干
使用工具
- PowerShell
- oh-my-posh
- Chocolatey
- Nerd Fonts
- PowerShellGet
- PSReadLine
操作步骤复述
去 微软商店 下载一个额外的 PowerShell
为了隔离环境,保证不会把终端搞炸
再去下载 Chocolatey 一个 Windows 应用管理器,方便对这些内容进行管理
再使用 Chocolatey 安装 Nerd Fonts
这个的字体作用是为了能够支持 oh-my-posh 的特殊图标。推荐使用 CodeNewRoman Nerd Font Mono ,还有个而外发现的 ComicShannsMono Nerd Font ,但是这个只支持 [A-Za-z0-9~!@#$%^&*()_+{}|[]\;:’”,.<>?/] ,稍微有点可惜
在 Windows Terminal 里面把 PowerShell 更改为
默认启动
的选项,在命令行
参数的最后面加上-nologo
加快载入速度,在字体
选择CodeNewRoman Nerd Font Mono
使用 Chocolatey 安装 oh-my-posh
为 PowerShell 复制以下创建配置文件
1
2
3
4
5
6
7
8# 创建配置文件
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
# 打开配置文件
notepad $PROFILE
# 写入 PowerShell 的打开默认打开行为
oh-my-posh init pwsh --config ~/.omp.theme.json | Invoke-Expression在 oh-my-posh 的主题中找到自己喜欢的配色,把配置文件 json 内容复制进自己用户目录下的需要手动创建的文件
.omp.theme.json
中更新 PowerShellGet 到最新版本
PowerShellGet 是 PowerShell 自带的包管理器与 npm pip 类似
使用 PowerShellGet 安装 PSReadLine
再回到刚才
notepad $PROFILE
的 .ps1 文件中,添加以下内容1
2
3
4
5#Tab 键会出现自动补全菜单
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
# 上下方向键箭头,搜索历史中进行自动补全
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
附录
set-ExecutionPolicy RemoteSigned
:ExecutionPolicy 是 PowerShell 中用于设置脚本执行权限的命令,下面是对它的详细解析:策略类型 含义 Restricted
默认,禁止所有脚本运行(只允许交互命令) AllSigned
所有脚本(本地或远程)都必须有受信任签名 RemoteSigned
本地脚本 ✅,远程脚本必须有签名 ✅ Unrestricted
所有脚本都能运行(运行远程脚本会有提示) Bypass
完全不检查,通常用于自动化或 CI 场景 Undefined
没有设置,继承系统默认策略 - 本次实践的指导为这篇文章 Windows 终端美化