此模組取代了版本 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 許可證獲得許可。
在參與此項目之前,請參閱我們的行為準則。
任何安全問題,請參閱我們的安全政策。