Cmdlets

Choose a cmdlet from the list on the left or search for a specific cmdlet. Choose a cmdlet from the list or search for a specific cmdlet.
About Help  Providers
 

Set-PSReadlineKeyHandler

Set-PSReadlineKeyHandler

psreadline.dll

Synopsis

Binds or rebinds keys to user defined or PSReadline provided key handlers.

Syntax

Set-PSReadlineKeyHandler [-Chord] [-ScriptBlock] [-BriefDescription] [-Description] [<CommonParameters>]

Set-PSReadlineKeyHandler [-Chord] [-Function] [<CommonParameters>]

Detailed Description

This cmdlet is used to customize what happens when a particular key or sequence of keys is pressed while PSReadline is reading input. With user defined key bindings, you can do nearly anything that is possible from a PowerShell script. Typically you might just edit the command line in some novel way, but because the handlers are just PowerShell scripts, you can do interesting things like change directories, launch programs, etc.

Parameters

-Chord <String[]>

The key or sequence of keys to be bound to a Function or ScriptBlock. A single binding is specified with a single string. If the binding is a sequence of keys, the keys are separated with a comma, e.g. "Ctrl+X,Ctrl+X". Note that this parameter accepts multiple strings. Each string is a separate binding, not a sequence of keys for a single binding.

Aliases

None

Required?

true

Position

0

Default value

None

Accept pipeline input?

false

Accept wildcard characters?

false

-ScriptBlock <ScriptBlock>

The ScriptBlock is called when the Chord is entered. The ScriptBlock is passed one or sometimes two arguments. The first argument is the key pressed (a ConsoleKeyInfo.) The second argument could be any object depending on the context.

Aliases

None

Required?

true

Position

1

Default value

None

Accept pipeline input?

false

Accept wildcard characters?

false

-BriefDescription <String>

A brief description of the key binding. Used in the output of cmdlet Get-PSReadlineKeyHandler.

Aliases

None

Required?

false

Position

named

Default value

None

Accept pipeline input?

false

Accept wildcard characters?

false

-Description <String>

A more verbose description of the key binding. Used in the output of the cmdlet Get-PSReadlineKeyHandler.

Aliases

None

Required?

false

Position

named

Default value

None

Accept pipeline input?

false

Accept wildcard characters?

false

-Function <String>

The name of an existing key handler provided by PSReadline. This parameter allows one to rebind existing key bindings or to bind a handler provided by PSReadline that is currently unbound. Using the ScriptBlock parameter, one can achieve equivalent functionality by calling the method directly from the ScriptBlock. This parameter is preferred though - it makes it easier to determine which functions are bound and unbound.

Aliases

None

Required?

true

Position

1

Default value

None

Accept pipeline input?

false

Accept wildcard characters?

false

Input Type


Return Type


Notes

Examples

-------------- Example 1 --------------

This command binds the up arrow key to the function HistorySearchBackward which will use the currently entered command line as the beginning of the search string when searching through history.

PS C:\> Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward

-------------- Example 2 --------------

This example binds the key Ctrl+Shift+B to a script block that clears the line, inserts build, then accepts the line. This example shows how a single key can be used to execute a command.

PS C:\> Set-PSReadlineKeyHandler -Chord Shift+Ctrl+B -ScriptBlock {
    [PSConsoleUtilities.PSConsoleReadLine]::RevertLine()
    [PSConsoleUtilities.PSConsoleReadLine]::Insert('build')
>>>     [PSConsoleUtilities.PSConsoleReadLine]::AcceptLine()
}

about_PSReadline