Useful Shortcuts from PSReadLine PowerShell Module
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:
- The Search for a Better PowerShell Console Experience
- A Better PowerShell Command-Line Edit
- Better PowerShell History Management with PSReadLine
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.
Abort the current action (that is, stop interactive history search). Does not cancel input like CancelLine.
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.
Like CharacterSearch, but searches backwards. With a negative argument , it searches forwards.
Clears the screen and displays the current prompt and input at the top of the screen.
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 *.
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.
Turn off demo mode.
Turn on demo mode. Displays a window under the input line that shows which keys are typed.
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.
Read a key or chord and display the key binding.
Show all of the currently bound keys.
Scroll the display up one screen.
Scroll the display down one screen.
Scroll the display to the top.
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:
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:
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:
Alt+?: WhatIsKey – Show the key binding for the next chord entered
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.
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 firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy