Skip to content

powershell 增强

posh-git

posh-git 可以为为 PowerShell 提供:

  • Git 仓库状态提示符(显示分支名、修改状态等)
  • Git 命令的自动补全

安装

输入以下命令安装 posh-git

shell
Install-Module posh-git -Scope CurrentUser

输入以下命令查看 posh-git 是否安装成功

shell
Get-Module posh-git -ListAvailable

配置

执行以下命令打开 PowerShell 配置文件

shell
Invoke-Item $profile

如果该文件不存在,可使用以下命令创建

shell
if (-not (Test-Path $profile)) { New-Item $profile -Force }

PowerShell 配置文件中添加以下代码,重新打开终端即可

shell
Import-Module posh-git

PSReadLine

PSReadLine 可以为为 PowerShell 提供:

  • 历史命令搜索
  • 智能提示和语法高亮

安装

PowerShell 5.1 及以上(包括 PowerShell 7+),一般不用手动安装,PSReadLine 已经是内置模块。 注意:内置的 PSReadLine 模块版本号必须大于等于 2.1,否则请手动安装。

输入以下命令查看 PSReadLine 是否安装成功

shell
Get-Module PSReadLine -ListAvailable

如果没有安装,输入以下命令手动安装 PSReadLine

shell
Install-Module PSReadLine -Scope CurrentUser -Force

配置

shell
# PSReadLine
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

完整配置参考

shell
# 导入 posh-git
Import-Module posh-git
# 配置 posh-git:只显示分支名
$GitPromptSettings.EnableFileStatus = $false

# PSReadLine 历史搜索
Import-Module PSReadLine -RequiredVersion 2.4.5
Set-PSReadLineOption -PredictionSource History
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

# fnm 自动切换 Node 版本
fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression

本站总访问量 --