June 19th, 2014

Useful Shortcuts from PSReadLine PowerShell Module

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy Ed Wilson talks about useful shortcuts from the PSReadLine Windows PowerShell module.

Microsoft Scripting Guy, Ed Wilson, is here. This is PSReadLine Week. You might also be interested in reading the following posts:

One of the really cool things about the PSReadLine Windows PowerShell module is all of the cool shortcut functions that become available. Here is a table of miscellaneous functions that are available.

Function

Key binding

Meaning

Abort

unbound

Abort the current action (that is, stop interactive history search). Does not cancel input like CancelLine.

CharacterSearch

<F3>

Read a key and search forward for that character.  With an argument, search forwards for the nth occurrence of that argument.  With a negative argument, it searches backwards.

CharacterSearchBackward

<Shift+F3>

Like CharacterSearch,  but searches backwards.  With a negative argument , it searches forwards.

ClearScreen

<Ctrl+L>

Clears the screen and displays the current prompt and input at the top of the screen.

DigitArgument

unbound

Used to pass numeric arguments to functions like CharacterSearch or YankNthArg. Alt+. Toggles the argument to be negative or non-negative.  For example, to enter 80 * characters, you could type Alt+8 Alt+0 *.

CaptureScreen

unbound

Copies selected lines to the clipboard in text and .rtf formats. Use Up and Down arrow keys to the first line to select,  then use Shift+Up arrow or Shift+Down arrow to select multiple lines.  After selecting, press ENTER to copy the text. Escape/Ctrl+C/Ctrl+G will cancel so nothing is copied to the clipboard.

DisableDemoMode

unbound

Turn off demo mode.

EnableDemoMode

unbound

Turn on demo mode. Displays a window under the input line that shows which keys are typed.

InvokePrompt

unbound

Erases the current prompt and calls the prompt function to redisplay the prompt. Useful for custom key handlers that change state, such as change the current directory.

WhatIsKey

<Alt+?>

Read a key or chord and display the key binding.

ShowKeyBindings

<Ctrl+Alt+?>

Show all of the currently bound keys.

ScrollDisplayUp

<PageUp>

Scroll the display up one screen.

ScrollDisplayDown

<PageDown>

Scroll the display down one screen.

ScrollDisplayTop

unbound

Scroll the display to the top.

ScrollDisplayToCursor

unbound

Scroll the display to the cursor.

One of the key combinations that I use often is the ShowKeyBindings function, which I access via Ctrl+Alt+?.

Note  When using the previous sequence, to get a ? on the U.S. English keyboard involves the Shift +? key combination. So the actual keys typed are <Ctrl><Alt><Shift><+>.

The output is shown here:

Image of command output

When I am getting ready to do a custom PSReadLine mapping, I use the WhatIs function to see how a key is mapped. To do this, I use the <Alt + ?> key combination. I then can type the key I am thinking about mapping and see if it is mapped. When I first type <Alt + ?>,  a prompt appears. I can then type the key combination I want. The prompt is shown here:

PS C:\>

what-is-key:

The insertion point is placed back at the Windows PowerShell prompt, and it blinks expecting input. I again type <Alt +?>, and it tells me that this combination is mapped to WhatIsKey. This is shown here:

PS C:\>

Alt+?: WhatIsKey – Show the key binding for the next chord entered

PS C:\>

Demo mode

I decide to map two keys to two functions. The first is EnableDemoMode and the second is DisableDemoMode. The key bindings are shown here:

Set-PSReadlineKeyHandler -Key ^E -Function EnableDemoMode

 Set-PSReadlineKeyHandler -Key ^D -Function DisableDemoMode

Demo mode is cool, and it is not only for demonstrations. It is useful as I experiment with using PSReadLine. It is also great for teaching Windows PowerShell classes and giving presentations. Why? Well, when Demo mode is turned on, it displays every key that is typed. This includes the Ctrl and the Alt keys, but not SHIFT.

When reading the Demo mode, realize that the most recently typed command is on the left side of the screen. In the following image, I first turned on WhatIsKey by typing Alt ?. Next I typed Ctrl D to see key mapping for that key combination.

Image of command output

That is all there is to using PSReadLine customizations. PSReadLine Week will continue tomorrow when I will talk about settings.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.