为 powershell 启用智能感知和主题

2022/10/12

# 通过安装 PSReadLine 即可

PSReadLine 为 PowerShell 控制台提供了强大的命令行编辑体验。 提供以下功能:

  • 命令行的语法着色
  • 语法错误的可视指示
  • 更好的多行体验 (编辑和历史记录)
  • 可自定义键绑定
  • Cmd 和 Emacs 模式
  • 许多配置选项
  • Cmd 模式下的 Bash 样式完成 (可选,emacs 模式默认为)
  • Emacs 洋克/杀环
  • 基于 PowerShell 令牌的“word”移动和删除
  • 预测 IntelliSense
  • 控制台中帮助的动态显示,无需在命令行上失去位置

参考 微软官方文档 (opens new window) 即可

如果已经安装了 powershell 7 则默认已安装,直接配置启用即可:

notepad $PROFILE
1

附上一个最简单的配置文件

# PSReadLine
Import-Module PSReadLine
# Enable Prediction History
Set-PSReadLineOption -PredictionSource History
# Advanced Autocompletion for arrow keys
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
1
2
3
4
5
6
7

# 安装主题

winget install JanDeDobbeleer.OhMyPosh -s winget
1

然后编辑配置文件

notepad $PROFILE
1

添加 oh-my-posh init pwsh | Invoke-Expression 即可

Last Updated: 2023-10-29T08:26:04.000Z