PowerShell Core美化

前置条件

安装PowerShell CoreVS Code

安装oh-my-posh posh-git

以管理员身份打开PowerShell, 执行

Install-Module oh-my-posh
Install-Module posh-git
Install-Module -Name PSReadLine -AllowPrerelease -Force

这里之所以要安装预览版PSReadLine是因为后续的PredictionViewStyle选项是2.2之后才支持的,而目前稳定版是2.1。 powershell core默认内置稳定版PSReadLine, 等下个版本应该就不用手动安装PSReadLine了

配置profile
执行 $profile 然后会显示profile文件所在路径
一般是%UserProfile%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
执行code $profile或者code-insiders $profile用vsc打开, 如果没有就会自动创建
内容如下:
设置为material主题

Import-Module posh-git
Import-Module oh-my-posh

Set-PoshPrompt -Theme material

Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
    param($commandName, $wordToComplete, $cursorPosition)
        dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
            [CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
}
Set-PSReadLineKeyHandler -Key "Ctrl+f" -Function ForwardWord
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
Set-PSReadLineOption -PredictionSource History -PredictionViewStyle ListView

可以通过命令Get-PoshThemes获取主题列表, 然后替换里边的material主题即可

效果如下,按上下键切换 自动提示列表来源于历史记录

原文地址:https://www.cnblogs.com/dygood/p/15785192.html