Windows Terminal + Powershell7,这才是终端该有样子。

Windows Terminal + Powershell7,这才是终端该有样子。

💡 原文中文,约3200字,阅读约需8分钟。
📝

内容提要

本文介绍了在Windows上使用Windows Terminal和Powershell7实现命令自动补全和自定义主题的方法,包括安装和配置步骤以及有用的插件和配色方案。

🔎

延伸解读

为什么选择 Windows Terminal + PowerShell 7

文章指出,Windows 自带的 PowerShell 和 cmd 缺乏命令自动补全等功能,而 cmder 虽好但仍有不足。Windows Terminal 与 PowerShell 7 的组合不仅能实现自动补全,还支持自定义主题,满足了作者对终端功能和个性化的需求。

安装与配置的关键步骤

安装 Windows Terminal 和 PowerShell 7 后,需在 Windows Terminal 设置中将默认配置文件设为 PowerShell 7。接着安装 oh my posh 和 Nerd 字体,并通过编辑 PowerShell 配置文件($Profile)来初始化 oh my posh 和加载插件,如 PSReadLine、posh-git 等。

个性化主题与配色方案

通过 Get-PoshThemes 命令可预览所有主题,修改配置文件中的主题名称即可切换。配色方案可在 Windows Terminal 的 JSON 文件中添加,如 Seafoam Pastel,并设置为默认。文章还推荐了其他配色网站,方便用户获取更多主题。

增强功能的 PowerShell 插件

PSReadLine 提供语法着色、命令补全和智能提示;posh-git 集成 Git 状态;Oh My Posh 美化提示符;ZLocation 支持快速跳转目录。安装后需在配置文件中导入模块,并可设置预测文本来源、Tab 补全等选项来优化体验。

❓

Q&A

如何在Windows Terminal中设置Powershell7为默认配置文件?

打开Windows Terminal设置(快捷键Ctrl+,),进入“启动”选项,将“默认配置文件”设置为Powershell7,并将“默认终端应用程序”设置为Windows终端。

如何安装和配置oh my posh来美化Powershell7终端?

从Microsoft Store搜索并安装oh my posh。然后在Powershell7中运行notepad $Profile,添加命令:oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\powerlevel10k_lean.omp.json" | Invoke-Expression,保存后运行. $Profile使配置生效。

如何为Windows Terminal添加自定义配色方案?

在Windows Terminal设置中打开JSON文件,在schemes数组里添加配色方案的JSON对象(例如Seafoam Pastel),保存后,在设置-配色方案中选择该方案并设为默认。

有哪些推荐的Powershell插件可以增强功能?

推荐PSReadLine(命令行编辑器,支持语法着色、补全等)、posh-git(Git集成)、Oh My Posh(美化提示符)、ZLocation(快速跳转目录)和gsudo(sudo for Windows)。

如何安装Powershell插件并配置?

在Powershell7中逐行运行安装命令,如Install-Module -Name PSReadLine -AllowPrerelease -Force等。然后运行notepad $Profile,添加Import-Module PSReadLine、Import-Module posh-git、Import-Module ZLocation,保存后运行. $Profile。

如何设置Powershell的智能提示和命令补全?

安装PSReadLine模块后,在$Profile中添加可选配置,如Set-PSReadLineOption -PredictionSource History启用历史记录预测,Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete设置Tab为菜单补全等。

🏷️

标签

➡️

继续阅读