Skip to content

Instantly share code, notes, and snippets.

@CN-CODEGOD
Created April 16, 2025 03:47
Show Gist options
  • Save CN-CODEGOD/6de8206df2c7d38e2dfab4a58bacd6b6 to your computer and use it in GitHub Desktop.
Save CN-CODEGOD/6de8206df2c7d38e2dfab4a58bacd6b6 to your computer and use it in GitHub Desktop.
powershell psreadline key handler
Description
The Set-PSReadLineKeyHandler cmdlet customizes the result when a key or sequence of keys is pressed. With user-defined key bindings, you can do almost anything that's possible from within a PowerShell script.
Examples
Example 1: Bind the arrow key to a function
This command binds the up arrow key to the HistorySearchBackward function. This function searches command history for command lines that start with the current contents of the command line.
PowerShell
Copy
Set-PSReadLineKeyHandler -Chord UpArrow -Function HistorySearchBackward
Example 2: Bind a key to a script block
This example shows how a single key can be used to run a command. The command binds the key Ctrl+b to a script block that clears the line, inserts the word "build", and then accepts the line.
PowerShell
Copy
Set-PSReadLineKeyHandler -Chord Ctrl+b -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert('build')
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment