# 如何在 PowerShell 中加入鍵盤快速鍵並模擬 Linux shell 下的操作體驗

我在 Windows **命令提示字元**有安裝 [Clink](https://mridgers.github.io/clink/) 工具，可以提供你在**命令提示字元** (CMD) 環境下也可以擁有跟 Bash 一樣的操作體驗。但在 Windows PowerShell 底下，就一直沒有類似的工具可以用，但是從 PowerShell 5.1 與 PowerShell Core 6 開始，就可以原生支援擴充 PowerShell 模式下的快速鍵，快來看看怎樣設定吧！

### 設定自訂的常用鍵盤快速鍵

1.  首先，開啟 Windows PowerShell 之後，可以先開啟 `$PROFILE` 檔案
    
    ```powershell
    notepad $PROFILE
    ```
    
    > 請注意：這裡的 `$PROFILE` 其實是一個變數，儲存的是指向 [PowerShell profile](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-6) 的檔案路徑，用來定義預設的環境變數、客製化執行環境、匯入其他模組、...等等。不同的 PowerShell 執行環境，就可能會指向到不同的 `$PROFILE` 檔案！
    
2.  再者，加入必要的 `Set-PSReadlineKeyHandler` 命令，設定鍵盤快速建與內建函式的組合
    
    我目前設定的鍵盤快速鍵並不多，只有以下 4 個，也是我最常用的 4 個：
    
    ```powershell
    # 設定按下 Ctrl+d 可以退出 PowerShell 執行環境
    Set-PSReadlineKeyHandler -Chord ctrl+d -Function ViExit
    
    # 設定按下 Ctrl+w 可以刪除一個單字
    Set-PSReadlineKeyHandler -Chord ctrl+w -Function BackwardDeleteWord
    
    # 設定按下 Ctrl+e 可以移動游標到最後面(End)
    Set-PSReadlineKeyHandler -Chord ctrl+e -Function EndOfLine
    
    # 設定按下 Ctrl+a 可以移動游標到最前面(Begin)
    Set-PSReadlineKeyHandler -Chord ctrl+a -Function BeginningOfLine
    ```
    
    設定完成！就是這麼簡單！
    

### 深入 `Set-PSReadLineKeyHandler` 命令 (Cmdlet)

1.  說明文件
    
    你可以透過以下命令，開啟 `Set-PSReadLineKeyHandler` 說明文件：
    
    ```powershell
    Get-Help Set-PSReadLineKeyHandler -Online
    ```
    
2.  設定鍵盤快速鍵
    
    一個最簡單的鍵盤快速鍵，就是加上一個 `-Chord` 參數，外加一個 `-Function` 參數。
    
    這裡比較需要特別說明的，就是 `-Function` 參數只能設定**內建的 [PSReadLine](https://github.com/PowerShell/PSReadLine) 函式**，這些函式你可以透過執行 `Get-PSReadLineKeyHandler` 命令，列出目前所有系統設定好的快速鍵清單，他會自動幫你分門別類，並且提供一個相當完整的對應表與中文說明，而且你會發現許多意外的、實用的內建快速鍵！
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/def28e050afa8ef469e4-70588302-38b5aa80-1c07-11ea-8f7d-2d98fb8bb5b9.png)
    
    基本上所有 [PSReadline](https://github.com/PowerShell/PSReadLine) 大致區分以下分類：
    
    -   基本編輯功能
    -   資料指標移動函式
    -   歷程記錄函式
    -   完成函式
    -   其他函式
    -   選取函式
    -   搜尋函式
3.  自訂複雜的組合命令
    
    假設你想在 PowerShell 視窗中按下 `Ctrl+Shift+B` 組合鍵，就自動輸出 `dotnet build -c Release` 命令，那麼你可以試試以下設定：
    
    ```powershell
    Set-PSReadLineKeyHandler -Chord Ctrl+Shift+B -ScriptBlock {
        [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
        [Microsoft.PowerShell.PSConsoleReadLine]::Insert('dotnet build -c Release')
        [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
    }
    ```
    
    設定完成後，直接可以按下 `Ctrl+Shift+B` 自動輸出建置命令並執行，真是太帥啦！
    
    > 這個 `Ctrl+Shift+B` 組合鍵同時也是 Visual Studio Code / Visual Studio 常用的建置命令！
    

### 相關連結

-   [PSReadLine](https://github.com/PowerShell/PSReadLine)
-   [Set-PSReadLineKeyHandler](https://learn.microsoft.com/zh-tw/powershell/module/PSReadline/Set-PSReadlineKeyHandler?view=powershell-5.1)
-   [Get-PSReadLineKeyHandler](https://learn.microsoft.com/zh-tw/powershell/module/PSReadline/Get-PSReadlineKeyHandler?view=powershell-5.1)
-   [sequence of keys to be bound to a function not working · Issue #1242 · PowerShell/PSReadLine](https://github.com/PowerShell/PSReadLine/issues/1242)
-   [$Profile - PowerShell - SS64.com](https://ss64.com/ps/syntax-profile.html)
