简 述: 美化 Windows-Terminal + OhMyPosh + PowerShell 7
来作为 windows 下的主力终端。

[TOC]
本文初发于 “偕臧的小站“,同步转载于此。
背景
半年前,在 上一篇 中详细介绍了 Windows-Terminal + OhMyPosh + GitBash
的美化 ,win10 下终端算是勉强可以使用了,颜值关勉强过了。
但是每次启动速度感觉很慢,使用 git
命令亦明显很是卡顿的感觉。自己的笔记本启动需 1-2 s,而公司电脑通常 5-6s,前面了解到 pwoshell 后,今天决定不能忍;遂切 PowerShell 7 + Windows-Terminal,最后效果来看,颜值和速度都有了✨。
介绍
win10 21H2 默认配置的是 PowerShell 5,约 2016 年;推荐使用更新的 PowerShell 7 ,对应 2022 年。此两版本差异很大,疑惑自行翻阅资料。对于未曾接触过此工具的,可参考之前写的这一篇 shell、bash、cmd、PowerShell的区别。
正文
美化步骤: 💻 win10 21H2
📎 PowerShell 7.2.2
下载 PowerShell-7.2.2-win-x64.msi 安装至本机
安装
Fira Code Nerd Font
字体;避免某些符号显示为 □ ;打开
PowerShell 7
,执行命令如下(后面执行命令亦都是这里执行)# 1. 安装 PSReadline 包,该插件可以让命令行很好用,类似 zsh Install-Module -Name PSReadLine -Scope CurrentUser # 2. 安装 posh-git 包,让你的 git 更好用 Install-Module posh-git -Scope CurrentUser # 3. 安装 oh-my-posh 包,让你的命令行更酷炫、优雅 Install-Module oh-my-posh -Scope CurrentUser
修改配置文件
settings.json
,将如下代码添加到对应的地方// ---------------------添加新的标签页,添加到 "profiles - list" 下--------------------- { "backgroundImageOpacity": 0.71999999999999997, "closeOnExit": "graceful", "colorScheme": "HomebrewCus", "commandline": "C:/Program Files/PowerShell/7/pwsh.exe", "cursorColor": "#FFFFFF", "cursorShape": "bar", "font": { "face": "FiraCode Nerd Font" }, "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}", "hidden": false, "historySize": 9001, "name": "PowerShell 7", "opacity": 89, "padding": "25", "snapOnInput": true, "source": "Windows.Terminal.PowershellCore", "startingDirectory": ".", "useAcrylic": true }, // ---------------------新增主题配色,添加到 "schemes" 下--------------------- { "background": "#283033", "black": "#000000", "blue": "#6666E9", "brightBlack": "#666666", "brightBlue": "#0E5CEE", "brightCyan": "#00E5E5", "brightGreen": "#00D900", "brightPurple": "#E500E5", "brightRed": "#E50000", "brightWhite": "#E5E5E5", "brightYellow": "#E5E500", "cursorColor": "#FFFFFF", "cyan": "#00A6B2", "foreground": "#00FF00", "green": "#00A600", "name": "HomebrewCus", "purple": "#B200B2", "red": "#FC5275", "selectionBackground": "#FFFFFF", "white": "#BFBFBF", "yellow": "#999900" },
添加 PowerShell 的启动参数
notepad.exe $Profile
提示文件不存在则新建,然后将此脚本拷贝后保存文本即可
<# * FileName: Microsoft.PowerShell_profile.ps1 * Date: 2022.04.08 * Copyright: No copyright. You can use this code for anything with no warranty. #> #------------------------------- Import Modules BEGIN ------------------------------- # 引入 posh-git Import-Module posh-git # 引入 oh-my-posh Import-Module oh-my-posh # 引入 ps-read-line Import-Module PSReadLine # 设置 PowerShell 主题 # Set-PoshPrompt ys Set-PoshPrompt paradox #------------------------------- Import Modules END ------------------------------- #------------------------------- Set Hot-keys BEGIN ------------------------------- # 设置预测文本来源为历史记录 Set-PSReadLineOption -PredictionSource History # 每次回溯输入历史,光标定位于输入内容末尾 Set-PSReadLineOption -HistorySearchCursorMovesToEnd # 设置 Tab 为菜单补全和 Intellisense Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete # 设置 Ctrl+d 为退出 PowerShell Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit # 设置 Ctrl+z 为撤销 Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置向上键为后向搜索历史记录 Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向下键为前向搜索历史纪录 Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward #------------------------------- Set Hot-keys END ------------------------------- #------------------------------- Functions BEGIN ------------------------------- # Python 直接执行 $env:PATHEXT += ";.py" # 更新系统组件 function Update-Packages { # update pip Write-Host "Step 1: 更新 pip" -ForegroundColor Magenta -BackgroundColor Cyan $a = pip list --outdated $num_package = $a.Length - 2 for ($i = 0; $i -lt $num_package; $i++) { $tmp = ($a[2 + $i].Split(" "))[0] pip install -U $tmp } # update TeX Live $CurrentYear = Get-Date -Format yyyy Write-Host "Step 2: 更新 TeX Live" $CurrentYear -ForegroundColor Magenta -BackgroundColor Cyan tlmgr update --self tlmgr update --all # update Chocolotey Write-Host "Step 3: 更新 Chocolatey" -ForegroundColor Magenta -BackgroundColor Cyan choco outdated } #------------------------------- Functions END ------------------------------- #------------------------------- Set Alias BEGIN ------------------------------- # 1. 编译函数 make function MakeThings { nmake.exe $args -nologo } Set-Alias -Name make -Value MakeThings # 2. 更新系统 os-update Set-Alias -Name os-update -Value Update-Packages # 3. 查看目录 ls & ll function ListDirectory { (Get-ChildItem).Name Write-Host("") } Set-Alias -Name ls -Value ListDirectory Set-Alias -Name ll -Value Get-ChildItem # 4. 打开当前工作目录 function OpenCurrentFolder { param ( # 输入要打开的路径 # 用法示例:open C:\ # 默认路径:当前工作文件夹 $Path = '.' ) Invoke-Item $Path } Set-Alias -Name open -Value OpenCurrentFolder #------------------------------- Set Alias END ------------------------------- #------------------------------- Set Network BEGIN ------------------------------- # 1. 获取所有 Network Interface function Get-AllNic { Get-NetAdapter | Sort-Object -Property MacAddress } Set-Alias -Name getnic -Value Get-AllNic # 2. 获取 IPv4 关键路由 function Get-IPv4Routes { Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'} } Set-Alias -Name getip -Value Get-IPv4Routes # 3. 获取 IPv6 关键路由 function Get-IPv6Routes { Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'} } Set-Alias -Name getip6 -Value Get-IPv6Routes #------------------------------- Set Network END -------------------------------
打开
Windows-Terminal
设置其为默认,开始享受额外其他 [可选]
更多 theme 预览:windowsterminalthemes.dev
- 几个个人比较喜欢的终端:Crystal、Violet、Cyberdyne、Duotone Dark、Flat、Hipster Green、Urple、Homebrew、和这个自己做修改的 HomebrewCus。
配置邮件菜单打开:How to Add Open Windows Terminal Here Option to Right-click Menu
安装
Scoop
包管理器 :ScoopInstaller/Scoop安装
winfetch
/winfetch
查看系统版本安装
winfetch
:Installation> Install-Script -Name pwshfetch-test-1 > Set-Alias winfetch pwshfetch-test-1 # Put this in $PROFILE
安装
winfetch
:> Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh') > scoop install neofetch

pwsh 提示更新
今天一打开终端,提示更新,可没有顺滑的更新命令;见 #8663
故不更新解决方案
You can set environment variable POWERSHELL_UPDATECHECK=LTS (or =Off).

注: 若细节不会,可参考 Windows-Terminal配置OhMyPosh来美化GitBash 一文。
- 本文链接:https://ifmet.cn/posts/5b69dcad/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。