PowerShell 命令补全

发布时间 2023-08-22 15:03:59作者: cralor

一、 PSReadLine

PSReadLine - Powershell 的强化工具

Powershell PSReadLine Complete Word(History) 根据历史补全词

psreadline是一个在PowerShell中使用的命令行编辑器模块。它提供了对命令行的自动完成、历史记录、语法高亮等功能的支持。

管理员运行终端安装PSReadLine。

Install-Module PSReadLine -Force

或者

# 先安装PowerShellGet
Install-Module -Name PowerShellGet -Force
# 然后再这条命令安装PSReadLine
Install-Module PSReadLine -AllowPrerelease -Force

2. 命令 $PROFILE 会显示配置文件的路径。

$PROFILE
# 编辑配置文件
notepad $PROFILE 或者
code $PROFILE

修改配置文件。

# PSReadLine
Import-Module PSReadLine
# 设置预测文本来源为历史记录
Set-PSReadLineOption -PredictionSource History

# 搜索历史时(↑)移动光标到行未而不是行首
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
# TAB补全时提供可选择菜单
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete
# 根据历史补全词(一次只补全部分)
Set-PSReadLineKeyHandler -Key "Ctrl+RightArrow" -Function ForwardWord
# Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销(默认已设置)

# 上下方向键搜索历史命令记录
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

二、git命令补全

美化PowerShell优化终端Terminal、在VS Code中配置Git Bash(二)

1.安装

Install-Module posh-git -Scope CurrentUser # posh-git

2 .配置文件中启用,code $PROFILE

Import-Module posh-git # 引入 posh-git