about_PSReadLine

簡短描述

PSReadLine 在 PowerShell 主控台中提供改善的命令列編輯體驗。

完整描述

PowerShell 7.2 隨附于 PSReadLine 2.1.0。 有較新版本可供使用。 目前版本的 PSReadLine 可以安裝及用於 Windows PowerShell 5.1 和更新版本。 針對某些功能,您必須執行 PowerShell 7.2 或更高版本。

PSReadLine 為 PowerShell 主控台提供功能強大的命令列編輯體驗。 它提供:

  • 命令列的語法著色
  • 語法錯誤的視覺指示
  • 編輯和歷程記錄) (更好的多行體驗
  • 可自訂的按鍵系結
  • Cmd 和 Emacs 模式
  • 許多組態選項
  • Cmd 模式中的 Bash 樣式完成 (選擇性,Emacs 模式中的預設值為)
  • Emacsyank/kill-ring
  • 以 PowerShell 權杖為基礎的「單字」移動和刪除
  • 預測性 IntelliSense
  • 主控台中的 [說明] 動態顯示,而不會遺失您在命令列上的位置

PSReadLine 需要 PowerShell 5.1 或更新版本。 PSReadLine 適用于預設的 Windows 主控台主機、視窗終端機和Visual Studio Code。 它無法在WINDOWS POWERSHELL ISE 中運作。

您可以從PowerShell 資源庫安裝 PSReadLine。 若要在支援的 PowerShell 版本中安裝 PSReadLine,請執行下列命令。

Install-Module -Name PSReadLine -AllowClobber -Force

注意

從 PowerShell 7.0 開始,如果偵測到螢幕助讀程式程式,PowerShell 會略過 Windows 上的自動載入 PSReadLine。 目前,PSReadLine 不適用於螢幕助讀程式。 Windows 上 PowerShell 7.0 的預設轉譯和格式設定正常運作。 如有必要,您可以手動載入模組。

預測性 IntelliSense

預測性 IntelliSense 是索引標籤完成的概念,可協助使用者順利完成命令。 它可讓使用者根據使用者歷程記錄的比對預測和其他網域特定外掛程式,探索、編輯和執行完整命令。

啟用預測性 IntelliSense

預設會停用預測性 IntelliSense。 若要啟用預測,只要執行下列命令:

Set-PSReadLineOption -PredictionSource History

PredictionSource參數也可以接受網域特定和自訂需求的外掛程式。

若要停用預測性 IntelliSense,只要執行:

Set-PSReadLineOption -PredictionSource None

注意

PSReadLine 2.2.6 預設會啟用預測性 IntelliSense。 如需詳細資訊,請參閱下面的附注一節中的PSReadLine 版本歷程記錄

自訂按鍵系結

PSReadLine 支援使用 Cmdlet 的 Set-PSReadLineKeyHandler 自訂按鍵系結。 大部分自訂按鍵系結都會呼叫其中一個 可系結函式,例如

Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

您可以將 ScriptBlock 系結至金鑰。 ScriptBlock 可以執行許多您想要的動作。 一些有用的範例包括

  • 編輯命令行
  • 例如,開啟新的視窗 (說明)
  • 變更目錄而不變更命令列

ScriptBlock 會收到兩個引數:

  • $key - [ConsoleKeyInfo] 物件,這是觸發自訂系結的索引鍵。 如果您將相同的 ScriptBlock 系結至多個金鑰,而且需要根據金鑰執行不同的動作,您可以檢查 $key 。 許多自訂系結都會忽略這個引數。

  • $arg - 任意引數。 通常,這是使用者從 Key bindings DigitArgument 傳遞的整數引數。 如果您的系結不接受引數,請合理忽略此引數。

讓我們看看將命令列新增至歷程記錄的範例,而不需執行。 當您發現忘記執行某些動作,但不想重新輸入您已輸入的命令列時,這非常有用。

$parameters = @{
    Key = 'Alt+w'
    BriefDescription = 'SaveInHistory'
    LongDescription = 'Save current line in history but do not execute'
    ScriptBlock = {
      param($key, $arg)   # The arguments are ignored in this example

      # GetBufferState gives us the command line (with the cursor position)
      $line = $null
      $cursor = $null
      [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line,
        [ref]$cursor)

      # AddToHistory saves the line in history, but does not execute it.
      [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($line)

      # RevertLine is like pressing Escape.
      [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
  }
}
Set-PSReadLineKeyHandler @parameters

您可以在安裝于PSReadLine模組資料夾中的 檔案 SamplePSReadLineProfile.ps1 中看到更多範例。

大部分的按鍵系結會使用一些協助程式函式來編輯命令行。 這些 API 記載于 about_PSReadLine_Functions

備註

命令歷程記錄

PSReadLine 會維護歷程記錄檔,其中包含您從命令列輸入的所有命令和資料。 歷程記錄檔是名為 的 $($Host.Name)_history.txt 檔案。 在 Windows 系統上,歷程記錄檔會儲存在 $env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine 。 在非 Windows 系統上,歷程記錄檔會儲存在 $env:XDG_DATA_HOME/powershell/PSReadLine$HOME/.local/share/powershell/PSReadLine

歷程記錄可以包含敏感性資料,包括密碼。 PSReadLine 會嘗試篩選掉敏感性資訊。 包含下列字串的任何命令列都不會寫入歷程記錄檔。

  • 密碼
  • asplaiNtext
  • token
  • apikey
  • secret

PSReadLine 2.2.0 會以下列方式改善敏感性資料的篩選:

  • 使用剖析命令列 (AST) 的 PowerShell 抽象語法樹狀結構來尋找敏感性資料。
  • 使用 SecretManagement 模組中安全 Cmdlet 的允許清單,允許這些命令新增至歷程記錄。 allowlist 包含:
    • Get-Secret
    • Get-SecretInfo
    • Get-SecretVault
    • Register-SecretVault
    • Remove-Secret
    • Set-SecretInfo
    • Set-SecretVaultDefault
    • Test-SecretVault
    • Unlock-SecretVault
    • Unregister-SecretVault

例如,允許將下列命令寫入歷程記錄檔:

Get-Secret PSGalleryApiKey -AsPlainText # Get-Secret is in the allowlist
$token = Get-Secret -Name github-token -Vault MySecret
[MyType]::CallRestAPI($token, $url, $args)
$template -f $token

下列命令不會寫入歷程記錄檔:

$token = 'abcd' # Assign expr-value to sensitive variable name.
Set-Secret abc $mySecret # Set-Secret is not in the allowlist.
ConvertTo-SecureString stringValue -AsPlainText # '-AsPlainText' is an alert.
Invoke-WebRequest -Token xxx # Expr-value as argument to '-Token'.
Get-ResultFromTwo -Secret1 (Get-Secret -Name blah -AsPlainText) -Secret2 sdv87ysdfayf798hfasd8f7ha # '-Secret2' has expr-value argument.

PSReadLine 發行歷程記錄

自 Windows PowerShell 5.1 中隨附的版本以來,PSReadLine 有許多更新。

  • PowerShell 7.3 隨附于 PSReadLine 2.2.6
  • PowerShell 7.2.5 隨附于 PSReadLine 2.1.0
  • PowerShell 7.0.11 隨附于 PSReadLine 2.0.4
  • PowerShell 5.1 隨附于 PSReadLine 2.0.0

如需變更的完整清單,請參閱 PSReadLine ChangeLog

  • PSReadLine 2.2.6

    在此版本中,根據下列條件,預設會啟用預測性 IntelliSense 功能:

    • 如果支援虛擬終端機 (VT) ,且在 PowerShell 7.2 或更高版本中執行的 PSReadLine, PredictionSource 會設定為 HistoryAndPlugin
    • 如果支援 VT,且在 7.2 之前于 PowerShell 中執行的 PSReadLine, PredictionSource 會設定為 History
    • 如果不支援 VT, PredictionSource 會設定為 None
  • PSReadLine 2.2.5

    具有次要 Bug 修正的正式服務版本。

  • PSReadLine 2.2.2

    • PSReadLine 新增了兩個新的預測 IntelliSense 功能:
      • 已新增 PredictionViewStyle 參數,以允許選取新的 ListView
      • 已將 PSReadLine 連線至 CommandPrediction PowerShell 7.2 中引進的 API,以允許使用者匯入可轉譯自訂來源建議的預測模組。
    • 已更新為使用 1.0.0 版的 Microsoft.PowerShell.Pager 動態說明
    • 已改善敏感性歷程記錄專案的清除
    • 許多錯誤修正和較小的改善
  • PSReadLine 2.1.0

    此版本匯總自 2.0.4 版以來新增的下列增強功能:

    • 從命令歷程記錄新增預測性 IntelliSense 建議
    • 許多錯誤修正和 API 增強功能
  • PSReadLine 2.0.4

    此版本匯總自 2.0.0 版以來新增的下列增強功能:

    • 已將 -Chord 參數新增至 Get-PSReadLineKeyHandler ,以允許搜尋特定的按鍵系結

參與 PSReadLine 的意見反應 &

GitHub 上的 PSReadLine

歡迎在 GitHub 頁面上提交提取要求或提交意見反應。

另請參閱

  • PSReadLine 受到 GNU 讀取線 程式庫的影響。