此模块取代了版本 3 及更高版本的 PowerShell 的命令行编辑体验。它提供:
“开箱即用”的体验对于 PowerShell 用户来说是非常熟悉的 - 不需要学习任何新的击键。
关于PSReadLine
的一些好资源:
PSReadLine
写了一篇精彩的介绍(2013)。PSReadLine
上撰写了一系列文章 (2014-2015)。PSReadLine
。 有多种方法可以安装PSReadLine
。
您将需要1.6.0
或更高版本的PowerShellGet
才能安装最新预发行版本的PSReadLine
。
Windows PowerShell 5.1 附带旧版本的PowerShellGet
,它不支持安装预发布模块,因此 Windows PowerShell 用户需要通过从提升的 Windows PowerShell 会话运行以下命令来安装最新的PowerShellGet
(如果尚未):
Install-Module - Name PowerShellGet - Force
Exit
安装PowerShellGet
后,您可以通过运行获取PSReadLine
的最新预发行版本
Install-Module PSReadLine - AllowPrerelease - Force
如果您只想获取最新的稳定版本,请运行:
Install-Module PSReadLine
[!NOTE] 预发布版本将具有更新的功能和错误修复,但也可能会引入新问题。
如果您在 Windows 10 上使用 Windows PowerShell 或使用 PowerShell 6+,则已安装PSReadLine
。最新 Windows 10 上的 Windows PowerShell 具有PSReadLine
版本2.0.0-beta2
。 PowerShell 6+ 版本具有较新的PSReadLine
预发行版本。
随着 PowerShell V3/V4 的 PowerShellGet 预览版的发布,不推荐从 GitHub 下载。我们不打算更新 GitHub 上的版本,并且可能会在某个时候从 GitHub 上完全删除该版本。
如果您使用的是 Windows PowerShell V5 或 V5.1 版本,或者使用 PowerShell 6+ 版本,则可以跳过此部分。
否则,您需要编辑您的配置文件才能导入该模块。有两个常用的配置文件,每个文件的说明略有不同。文件C:Users[User]DocumentsWindowsPowerShellprofile.ps1
用于所有主机(例如ISE
和powershell.exe
)。如果您已有此文件,则应添加以下内容:
if ( $host .Name -eq ' ConsoleHost ' )
{
Import-Module PSReadLine
}
或者,文件C:Users[User]DocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1
仅适用于powershell.exe
。使用此文件,您只需添加:
Import-Module PSReadLine
无论哪种情况,如果您还没有相应的文件,都可以创建该文件。
运行下面建议的命令之一时,请务必退出powershell.exe
、 pwsh.exe
或pwsh
的所有实例,包括在VSCode
终端中打开的实例。
然后,确保PSReadLine
未加载:
cmd.exe
、 powershell_ise.exe
或通过Win+R
快捷方式运行下面建议的命令;bash
或zsh
)运行下面建议的命令。如果您使用的是 Windows PowerShell 附带的PSReadLine
版本,则需要运行: powershell -noprofile -command "Install-Module PSReadLine -Force -SkipPublisherCheck -AllowPrerelease"
。注意:在运行此命令之前,您需要确保 PowershellGet 已更新。
如果您使用的是 PowerShell 6+ 版本附带的PSReadLine
版本,则需要运行: <path-to-pwsh-executable> -noprofile -command "Install-Module PSReadLine -Force -SkipPublisherCheck -AllowPrerelease"
。
如果您自己从 PowerShell 库安装了PSReadLine
,则只需运行: powershell -noprofile -command "Update-Module PSReadLine -AllowPrerelease"
或<path-to-pwsh-executable> -noprofile -command "Update-Module PSReadLine -AllowPrerelease"
,具体取决于您使用的 PowerShell 版本。
如果您收到类似以下错误:
Remove-Item : Cannot remove item
C:Users{yourName}DocumentsWindowsPowerShellModulesPSReadLineMicrosoft.PowerShell.PSReadLine.dll: Access to the path
'C:Users{yourName}DocumentsWindowsPowerShellModulesPSReadLineMicrosoft.PowerShell.PSReadLine.dll' is denied.
或类似的警告:
WARNING: The version '2.0.0' of module 'PSReadLine' is currently in use. Retry the operation after closing the applications.
那么您没有杀死所有加载PSReadLine
的进程。
要开始使用,只需导入模块:
Import-Module PSReadLine
要使用 Emacs 键绑定,您可以使用:
Set-PSReadLineOption - EditMode Emacs
要查看当前的按键绑定:
Get-PSReadLineKeyHandler
有很多配置选项,请参阅Set-PSReadLineOption
的选项。 PSReadLine
提供有关其 cmdlet 的帮助以及about_PSReadLine
主题 - 请参阅这些主题以获取更详细的帮助。
要设置您自己的自定义键绑定,请使用 cmdlet Set-PSReadLineKeyHandler
。例如,为了获得更好的历史体验,请尝试:
Set-PSReadLineKeyHandler - Key UpArrow - Function HistorySearchBackward
Set-PSReadLineKeyHandler - Key DownArrow - Function HistorySearchForward
通过这些绑定,如果当前命令行为空,则向上箭头/向下箭头将像 PowerShell/cmd 一样工作。如果您输入了一些文本,它将在历史记录中搜索以当前输入的文本开头的命令。
要启用 bash 样式完成而不使用 Emacs 模式,您可以使用:
Set-PSReadLineKeyHandler - Key Tab - Function Complete
这是一个更有趣的例子,说明了可能性:
Set-PSReadLineKeyHandler - Chord ' " ' , " ' " `
- BriefDescription SmartInsertQuote `
- LongDescription " Insert paired quotes if not already on a quote " `
- ScriptBlock {
param ( $key , $arg )
$line = $null
$cursor = $null
[ Microsoft.PowerShell.PSConsoleReadLine ]::GetBufferState([ ref ] $line , [ ref ] $cursor )
if ( $line .Length -gt $cursor -and $line [ $cursor ] -eq $key .KeyChar ) {
# Just move the cursor
[ Microsoft.PowerShell.PSConsoleReadLine ]::SetCursorPosition( $cursor + 1 )
}
else {
# Insert matching quotes, move cursor to be in between the quotes
[ Microsoft.PowerShell.PSConsoleReadLine ]::Insert( " $ ( $key .KeyChar ) " * 2 )
[ Microsoft.PowerShell.PSConsoleReadLine ]::GetBufferState([ ref ] $line , [ ref ] $cursor )
[ Microsoft.PowerShell.PSConsoleReadLine ]::SetCursorPosition( $cursor - 1 )
}
}
在此示例中,当您键入单引号或双引号时,可能会发生两种情况。如果光标后面的字符不是键入的引号,则插入一对匹配的引号,并将光标放置在匹配的引号内。如果光标后面的字符是键入的引号,则光标将简单地移过引号而不会插入任何内容。如果您使用 Resharper 或其他智能编辑器,这种体验将会很熟悉。
请注意,通过以这种方式编写的处理程序,它可以正确处理撤消 - 两个引号都将通过一次撤消来撤消。
示例配置文件有很多很棒的示例可供查看。安装PSReadLine
时包含此文件。
请参阅[Microsoft.PowerShell.PSConsoleReadLine]
的公共方法,了解您可以修改哪些其他内置功能。
如果您想在自定义键绑定中以某种未实现的方式更改命令行,您可以使用以下方法:
[ Microsoft.PowerShell.PSConsoleReadLine ]::GetBufferState
[ Microsoft.PowerShell.PSConsoleReadLine ]::Insert
[ Microsoft.PowerShell.PSConsoleReadLine ]::Replace
[ Microsoft.PowerShell.PSConsoleReadLine ]::SetCursorPosition
如何开发和贡献请参阅贡献指南。
要在 Windows、Linux 或 macOS 上构建PSReadLine
,您必须安装以下软件:
InvokeBuild
和platyPS
构建脚本build.ps1
可用于引导、构建和测试项目。
./build.ps1 -Bootstrap
./build.ps1 -Configuration Debug -Framework net462
./build.ps1 -Configuration Debug -Framework netcoreapp2.1
./build.ps1 -Test -Configuration Debug -Framework net462
./build.ps1 -Test -Configuration Debug -Framework netcoreapp2.1
构建后,生成的工件可以在<your-local-repo-root>/bin/Debug
中找到。为了将导入的模块与本地构建的模块隔离,请务必运行pwsh -NonInteractive -NoProfile
,以免自动加载已安装的默认 PSReadLine 模块。然后,通过Import-Module <your-local-repo-root>/bin/Debug/PSReadLine/PSReadLine.psd1
加载本地构建的 PSReadLine 模块。
更改日志可在此处获取。
PSReadLine 根据 2-Clause BSD 许可证获得许可。
参与此项目之前,请参阅我们的行为准则。
对于任何安全问题,请参阅我们的安全政策。