Windows 下完美的 Terminal:Powershell

Windows 下完美的 Terminal:Powershell

December 11, 2022
分享 ,

以前用命令行工具一直都是全手打,没用过提示之类的,但是看到一些大佬的视频里用终端都带提示、历史命令,那个时候就很好奇,不过一直没学习怎么配置。

前些日志在折腾 Python 环境的时候偶然接触了 Windows 下的跨平台命令行工具:Powershell(这个不是 Windows 自带的那个 Windows Powershell),这个 Powershell 项目网站上指出的两者区别。

Windows PowerShell vs. PowerShell Core
Although this repository started as a fork of the Windows PowerShell code base, changes made in this repository do not make their way back to Windows PowerShell 5.1 automatically. This also means that issues tracked here are only for PowerShell Core 6 and higher. Windows PowerShell specific issues should be reported with the Feedback Hub app, by choosing "Apps > PowerShell" in category.

用上了这个工具后我又偶然了解到 oh-my-posh ,了解了 Trminal 的美化,然后又偶然间又接触到了命令行提示的配置,配置完后才发现配置好的 Trminal 和没配置的简直是一个天上一个地上。

一、安装 Powershell

安装方式又很多种,我这里用的官方推荐的 winget 安装方式 [2]

winget search Microsoft.PowerShell
winget install --id Microsoft.Powershell --source winget

二、安装 oh-my-posh

windows11 的话直接用 Winget 执行安装命令 [1] 就可以了,还是相当方便的

winget install JanDeDobbeleer.OhMyPosh -s winget

三、修改配置文件

使用 notepad $profile 打开配置文件,在开头部分增加以下代码,其中 catppuccin_frappe.omp.json 为主题名字,具体可以用的主题在这个网站上可以预览:

#配置oh-my-posh自动加载,并且设置主题为 catppuccin_frappe 
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\catppuccin_frappe.omp.json" | Invoke-Expression

# 下面是配置自动完成和自动提示
# Shows navigable menu of all options when hitting Tab
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
# Autocompletion for arrow keys
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
# auto suggestions
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History


引用

Windows | Oh My Posh ↩︎

在 Windows 上安装 PowerShell - PowerShell | Microsoft Learn ↩︎

加入评论