Win11下安装PSReadLine终端自动补全
PowerShell 7.2 附带 PSReadLine 2.1.0。 有较新版本可用。 可以在 Windows PowerShell 5.1 及更新版本上安装和使用 PSReadLine 的当前版本。 对于某些功能,需要运行 PowerShell 7.2 或更高版本。
本文章在 Windows PowerShell 5.1 版本上安装操作
在PowerShell中输入查看版本:
$PSVersionTable
1. 安装“PSReadLine”
以管理员身份打开PowerShell
安装
Install-Module PSReadLine -RequiredVersion 2.1.0
执行策略更改
执行策略可以防止您执行不信任的脚本。更改执行策略可能会使您面临 about_Execution_Policies
帮助主题中所述的安全风险。是否要更改执行策略?
[Y] 是(Y) [N] 否(N) [S] 挂起(S) [?] 帮助 (默认值为“Y”): y
在最后输入Y
选择“Y”,安装成功!
2.初始化
重新打开终端
- 输入
Set-ExecutionPolicy Unrestricted
如果报错改为
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser```
- 选择“Y”
- 最后输入
Import-Module 'C:\Program Files\WindowsPowerShell\Modules\PSReadline\2.0.0\PSReadline.psd1’
win 10 对应的为
Import-Module 'C:\Program Files\WindowsPowerShell\Modules\PSReadline\1.2\PSReadline.psd1’
-初始化
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History
3. 检测是否有配置好文件
Test-path $profile
如果返回"false",输入:
New-item –type file –force $profile
4.编辑profile配置文件
编辑文件
notepad $profile
输入:
# 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
保存