Set-PSReadlineKeyHandler
Binds keys to user-defined or PSReadline key handler functions.
Syntax
ScriptBlock
Set-PSReadlineKeyHandler
[-ScriptBlock] <ScriptBlock>
[-Chord] <String[]>
[-BriefDescription <String>]
[-Description <String>]
[-ViMode <ViMode>]
[<CommonParameters>]
Function
Set-PSReadlineKeyHandler
[-Chord] <String[]>
[-Function] <String>
[-ViMode <ViMode>]
[<CommonParameters>]
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 is 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 function HistorySearchBackward. This function uses the current contents of the command line as the search string used to search the command history.
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+Shift+B to a script block that clears the line, inserts the word "build", and then accepts
the line.
Set-PSReadlineKeyHandler -Chord Ctrl+Shift+B -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert('build')
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
Parameters
-BriefDescription
A brief description of the key binding. This description is displayed by the
Get-PSReadlineKeyHandler cmdlet.
Parameter properties
| Type: | String |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
ScriptBlock
| Position: | Named |
| Mandatory: | False |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-Chord
The key or sequence of keys to be bound to a function or script block. Use a single string to specify a single binding. If the binding is a sequence of keys, separate the keys by a comma, as in the following example:
Ctrl+X,Ctrl+L
This parameter accepts an array of strings. Each string is a separate binding, not a sequence of keys for a single binding.
Parameter properties
| Type: | String[] |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
| Aliases: | Key |
Parameter sets
(All)
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-Description
Specifies a more detailed description of the key binding that is visible in the output of the
Get-PSReadlineKeyHandler cmdlet.
Parameter properties
| Type: | String |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
| Aliases: | LongDescription |
Parameter sets
ScriptBlock
| Position: | Named |
| Mandatory: | False |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-Function
Specifies the name of an existing key handler provided by PSReadline. This parameter lets you rebind existing key bindings, or bind a handler that is currently unbound.
Parameter properties
| Type: | String |
| Default value: | None |
| Accepted values: | Abort, AcceptAndGetNext, AcceptLine, AddLine, BackwardChar, BackwardDeleteChar, BackwardDeleteLine, BackwardDeleteWord, BackwardKillLine, BackwardKillWord, BackwardWord, BeginningOfHistory, BeginningOfLine, CancelLine, CaptureScreen, CharacterSearch, CharacterSearchBackward, ClearHistory, ClearScreen, Complete, Copy, CopyOrCancelLine, Cut, DeleteChar, DeleteCharOrExit, DeleteEndOfWord, DeleteLine, DeleteLineToFirstChar, DeleteToEnd, DeleteWord, DigitArgument, EndOfHistory, EndOfLine, ExchangePointAndMark, ForwardChar, ForwardDeleteLine, ForwardSearchHistory, ForwardWord, GotoBrace, GotoColumn, GotoFirstNonBlankOfLine, HistorySearchBackward, HistorySearchForward, InsertLineAbove, InsertLineBelow, InvertCase, InvokePrompt, KillLine, KillRegion, KillWord, MenuComplete, MoveToEndOfLine, NextHistory, NextLine, NextWord, NextWordEnd, Paste, PasteAfter, PasteBefore, PossibleCompletions, PrependAndAccept, PreviousHistory, PreviousLine, Redo, RepeatLastCharSearch, RepeatLastCharSearchBackwards, RepeatLastCommand, RepeatSearch, RepeatSearchBackward, ReverseSearchHistory, RevertLine, ScrollDisplayDown, ScrollDisplayDownLine, ScrollDisplayToCursor, ScrollDisplayTop, ScrollDisplayUp, ScrollDisplayUpLine, SearchChar, SearchCharBackward, SearchCharBackwardWithBackoff, SearchCharWithBackoff, SearchForward, SelectAll, SelectBackwardChar, SelectBackwardsLine, SelectBackwardWord, SelectForwardChar, SelectForwardWord, SelectLine, SelectNextWord, SelectShellBackwardWord, SelectShellForwardWord, SelectShellNextWord, SelfInsert, SetMark, ShellBackwardKillWord, ShellBackwardWord, ShellForwardWord, ShellKillWord, ShellNextWord, ShowKeyBindings, SwapCharacters, TabCompleteNext, TabCompletePrevious, Undo, UndoAll, UnixWordRubout, ValidateAndAcceptLine, ViAcceptLine, ViAcceptLineOrExit, ViAppendLine, ViBackwardDeleteGlob, ViBackwardGlob, ViBackwardWord, ViCommandMode, ViDeleteBrace, ViDeleteEndOfGlob, ViDeleteGlob, ViDigitArgumentInChord, ViEditVisually, ViExit, ViGotoBrace, ViInsertAtBegining, ViInsertAtEnd, ViInsertLine, ViInsertMode, ViInsertWithAppend, ViInsertWithDelete, ViJoinLines, ViNextWord, ViSearchHistoryBackward, ViTabCompleteNext, ViTabCompletePrevious, ViYankBeginningOfLine, ViYankEndOfGlob, ViYankEndOfWord, ViYankLeft, ViYankLine, ViYankNextGlob, ViYankNextWord, ViYankPercent, ViYankPreviousGlob, ViYankPreviousWord, ViYankRight, ViYankToEndOfLine, ViYankToFirstChar, WhatIsKey, Yank, YankLastArg, YankNthArg, YankPop |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
Function
| Position: | 1 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-ScriptBlock
Specifies a script block value to run when the chord is entered. PSReadline passes one or two parameters to this script block. The first parameter is a ConsoleKeyInfo object representing the key pressed. The second argument can be any object depending on the context.
Parameter properties
| Type: | ScriptBlock |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
ScriptBlock
| Position: | 1 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-ViMode
Specify which vi mode the binding applies to.
Valid values are:
- Insert
- Command
Parameter properties
| Type: | ViMode |
| Default value: | None |
| Accepted values: | Insert, Command |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
(All)
| Position: | Named |
| Mandatory: | False |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
Inputs
None
You cannot pipe objects to this cmdlet.