当前位置: 首页 > 工具软件 > Oh My Posh > 使用案例 >

powershell+oh-my-posh PSReadLine 代码提示配置

程瑞
2023-12-01

升级 powershell

powershell版本要大于5.1,windows自带5.1,查看软件版本:

$PSVersionTable

搜索软件包:

winget search powershell

显示:

~\Desktop ❯ winget search powershell
搜索源时失败;结果将不包括在内: msstore
名称                     ID                                  版本         匹配            源
------------------------------------------------------------------------------------------------
PowerShell               Microsoft.PowerShell                7.2.2.0                      winget
Windows Terminal Preview Microsoft.WindowsTerminal.Preview   1.13.10733.0 Tag: PowerShell winget
Windows Terminal         Microsoft.WindowsTerminal           1.12.10732.0 Tag: powershell winget
PowerShell Preview       Microsoft.PowerShell.Preview        7.3.0.3      Tag: powershell winget
ConEmu                   Maximus5.ConEmu                     11.220.4180  Tag: powershell winget
EasyConnect              lstratman.easyconnect               3.1.0.105    Tag: powershell winget
Oh My Posh               JanDeDobbeleer.OhMyPosh             7.71.2       Tag: powershell winget
TfsCmdlets               Igoravl.TfsCmdlets                  2.3.1.2724   Tag: powershell winget
electerm                 electerm.electerm                   1.21.34      Tag: powershell winget
wol                      DarkfullDante.wol                   1.0.2        Tag: powershell winget
AutomatedLab             AutomatedLab.AutomatedLab           5.41.0       Tag: powershell winget
PowerShell Universal     IronmanSoftware.PowerShellUniversal 2.6.2                        winget

id为Microsoft开头的就是官方版本,下载最新版本powershell:

winget install Microsoft.PowerShell

安装完后开始菜单程序列表会出现刚刚安装的powershell,用管理员打开新的powershell,设置策略:

set-executionpolicy RemoteSigned

查看当前策略:

$OutputEncoding

如果是RemoteSigned,则设置成功。

Restricted:禁止运行任何脚本和配置文件
AllSigned :可以运行脚本,但要求所有脚本和配置文件由可信发布者签名,包括在本地计算机上编写的脚本。
RemoteSigned :可以运行脚本,但要求从网络上下载的脚本和配置文件由可信发布者签名; 不要求对已经运行和已在本地计算机编写的脚本进行数字签名。
Unrestricted :可以运行未签名脚本。(危险!)

如果不设置策略,powershell将不能运行外部未签名脚本。

查看是否有powershell配置文件

Test-path $profile

如果显示为false,手动创建一个:

New-item –type file –force $profile

如果为true,则修改显示路径的配置文件。也可以使用$PROFILE查看配置文件路径。在配置文件里新增如下内容:

# powershell初始化加载 PSReadLine 模块
Import-Module PSReadLine
# 使用历史记录进行脚本提示
Set-PSReadLineOption -PredictionSource History

重启powershell,就可以获得oh-my-zsh里zsh-autosuggestions一样的体验了。Autosuggestion除了可以使用键补全全部外,还可以使用option + →选择下一个单词,现在为powershell配置同样的功能,修改$PROFILE如下:

# powershell初始化加载 PSReadLine 模块
Import-Module PSReadLine
# 使用历史记录进行脚本提示
Set-PSReadLineOption -PredictionSource History
# alt在windows中有特殊用途,这里使用ctrl键代替
Set-PSReadLineKeyHandler -Chord "Ctrl+RightArrow" -Function ForwardWord

现在按下ctrl + →可以逐字补全了。下面开始换powershell主题。

安装 oh-my-posh

用过Linux甚至是mac后的开发者,再回来用windows最不爽的一点可能就是开发环境的配置问题了。在linux各个发行版都有自己的包管理器,提供最稳定或者最新的软件包,开发者大多数情况下只需要一行命令install一下就行,然后就可以在ide或者工具里使用刚刚安装好的库了。然后这一切在windows下边的有点儿尴尬,在winget项目还没有立项之前,windows官方是希望每一家公司自己负责自己程序的安装和更新的,因此每个人都需要自己去每一家的官网上下载msi或者exe文件自己安装,而且一般商业性质的软件还会常驻一个updater在后台,久而久之,安装的东西多了,系统免不齐会运行的越来越慢。

因此,Windows的开发者们就产生了自己做包管理器的想法,于是社区两大著名的windows包管理器:Chocolatey和Scoop就诞生了,相比之下,Chocolatey臃肿庞大,自带GUI界面,相当于是一个进化版的windows商店……

Scoop是后起之秀,特点是轻量绿色化,scoop是按照aptget或者zypper这样的linux包管理理念设计的一个包管理器。本身不存储安装程序,而是将安装逻辑和安城程序的获取url写进配置文件再上传至github的repo上(有点儿像AUR的那种感觉)。这样社区就可以给scoop方便的进行贡献,只需要提交PR就好了。而且scoop多半是绿色安装,安装在每一个用户的用户文件夹里,大多数收录的程序安装时不会触发UAC获取管理员权限。软件的安装位置固定,卸载彻底,软件环境自动写进环境变量,不会对系统造成破坏。(总之就是好处很多)

Reference

回到Windows,最全教程让你的Powershell更好看且好用(2021版)


安装scoop

iwr -useb get.scoop.sh | iex

安装oh-my-posh

scoop install https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json

主题路径:

C:\Users\username\scoop\apps\oh-my-posh\current\themes

在这个网站选一个好看的主题:

Themes | Oh My Posh


在配置文件里添加:

oh-my-posh --init --shell pwsh --config C:\Users\username\scoop\apps\oh-my-posh\current\themes\主题的名字.omp.json | Invoke-Expression

username按需修改。我选的主题是powerlevel10k_lean,所以配置文件加上

oh-my-posh --init --shell pwsh --config C:\Users\username\scoop\apps\oh-my-posh\current\themes\powerlevel10k_lean.omp.json | Invoke-Expression

重启,即可看到效果。

References

Oh My Posh:全平台终端提示符个性化工具 - 少数派

让你的windows shell更好用,打造类fish/zsh的powershell - 掘金

 类似资料: