CmdID / PowerShell / Cmdlets / Set
Set-PSReadLineKeyHandler
Binds keys to user-defined or PSReadLine key handler functions. PowerShell reference
From the PowerShell reference 6 parameters
What it does
Binds keys to user-defined or PSReadLine key handler functions. 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.
Set-PSReadLineKeyHandler [-ScriptBlock] <ScriptBlock> [-BriefDescription <String>] [-Description <String>] [-Chord] <String[]> [-ViMode <ViMode>] [<CommonParameters>]Set-PSReadLineKeyHandler [-Chord] <String[]> [-ViMode <ViMode>] [-Function] <String> [<CommonParameters>]
Bind the arrow key to a function
Set-PSReadLineKeyHandler -Chord UpArrow -Function HistorySearchBackwardIdentifyBind a key to a scriptblock
Set-PSReadLineKeyHandler -Chord Ctrl+b -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert('build')
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}IdentifyIts page in the reference: Set-PSReadLineKeyHandler.
Parameters
-BriefDescriptionA brief description of the key binding. This description is displayed by the
Get-PSReadLineKeyHandler cmdlet.-ChordThe key or sequence of keys to be bound to a function or scriptblock. Use a single string to specify a single binding. If the binding is a sequence of keys, separate the keys by a comma. For example: Ctrl+x,Ctrl+l
Letter key references are defined using lowercase letters. If you want to define a chord that uses an uppercase letter, the chord must include the Shift key. For example, Ctrl+Shift+x and Ctrl+x create different bindings.
This parameter accepts an array of strings. Each string is a separate binding, not a sequence of keys for a single binding.-DescriptionSpecifies a more detailed description of the key binding that's visible in the output of the
Get-PSReadLineKeyHandler cmdlet.-FunctionSpecifies the name of an existing key handler provided by PSReadLine. This parameter lets you change existing key bindings, or bind a handler that's currently unbound. Use Get-PSReadLineKeyHandler to get a list of function names. You can also use tab-completion with this parameter to see the available key handler functions.-ScriptBlockSpecifies a scriptblock value to run when the chord is entered. PSReadLine passes one or two parameters to this scriptblock. The first parameter is a ConsoleKeyInfo object representing the key pressed. The second argument can be any object depending on the context.-ViModeSpecify which vi mode the binding applies to.
Valid values are:
- Insert
- Command