Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

A Better PowerShell Command-Line Edit

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using PSReadLine to improve command-line editing in Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. Today I thought I would try something a bit different. It is hard to show command-line editing with screenshots and tables of shortcuts. So today’s post is basically two parts. The first part includes two tables that I built, which are basically reference material for the PSReadLine command-line enhancements.

The second part is a short (less than two minutes) video. I recorded it to illustrate a simple command-line edit. My video is nothing like the way cool Scripting Cmdlet Style video, but it should prove to be somewhat helpful.

Cursor movement

One of the cool things about PSReadLine is that it adds a lot of ways to move around in the Windows PowerShell console. Learning a few keyboard shortcuts makes me a lot more productive, and it also saves my wrists and hands from a lot of extra typing and work. Here is a table that provides the cursor movement shortcuts. For the most part, they are straightforward, and they are inline with standard keyboard shortcuts used in other places.

Cursor Movement

Shortcut

Meaning

EndOfLine  

<End>  

Move the cursor to the end of the input.

BeginningOfLine  

<Home>  

Move the cursor to the start of the input.

ForwardChar  

<Right Arrow>  

Move the cursor one character to the right. This may move the cursor to the next line of multiline input.

BackwardChar  

<Left Arrow>  

Move the cursor one character to the left. This may move the cursor to the previous line of multiline input.

ForwardWord  

unbound  

Move the cursor forward to the end of the current word, or if between words, to the end of the next word. Word delimiter characters can be set with:

Set-PSReadlineOption -WordDelimiters <string of delimiter characters>

NextWord  

<Ctrl+Right Arrow>  

Move the cursor forward to the start of the next word. Word delimiter characters can be set with:

Set-PSReadlineOption -WordDelimiters <string of delimiter characters>

BackwardWord  

<Ctrl+Left Arrow>  

Move the cursor back to the start of the current word, or if between words, to the start of the previous word. Word delimiter characters can be set with:

Set-PSReadlineOption -WordDelimiters <string of delimiter characters>

ShellForwardWord  

unbound  

Like ForwardWord except word boundaries are defined by Windows PowerShell token boundaries.

ShellNextWord  

unbound  

Like NextWord except word boundaries are defined by Windows PowerShell token boundaries.

ShellBackwardWord  

unbound  

Like BackwardWord except word boundaries are defined by Windows PowerShell token boundaries.

GotoBrace  

<Ctrl+}>  

Go to the matching parenthesis curly brace or square bracket.

AddLine  

<Shift-Enter>  

The continuation prompt is displayed on the next line, and PSReadLine waits for keys to edit the current input. This is useful to enter multiline input as a single command even when a single line is complete input by itself.

 Command-line editing

One of the real strengths of PSReadLine is the enhanced command-line editing experience. Learning a few shortcuts will vastly improve the command-line editing experience. Most of the shortcuts make sense and are familiar to me.

Edit Function

Shortcut

Meaning

CancelLine

unbound

Cancel all editing to the line and leave the line of input on the screen, but return from PSReadLine without executing the input.

RevertLine

<ESC>

Revert all of the input since the last input was accepted and executed. This is equivalent to doing Undo until there is nothing left to undo.

BackwardDeleteChar

<Backspace>

Delete the character before the cursor.

DeleteChar

<Delete>

Delete the character under the cursor.

AcceptLine

<Enter>

Attempt to execute the current input. If the current input is incomplete (for example, there is a missing closing parenthesis bracket or quotation mark), the continuation prompt is displayed on the next line and PSReadLine waits for keys to edit the current input.

AcceptAndGetNext

unbound

Like AcceptLine but after the line completes, start editing the next line from history.

BackwardDeleteLine

<Ctrl+Home>

Delete the text from the start of the input to the cursor.

ForwardDeleteLine

<Ctrl+End>

Delete the text from the cursor to the end of the input.

SelectBackwardChar

<Shift+Left Arrow>

Adjust the current selection to include the previous character.

SelectForwardChar

<Shift+Right Arrow>

Adjust the current selection to include the next character.

SelectBackwardWord

<Shift+Ctrl+Left Arrow>

Adjust the current selection to include the previous word.

SelectForwardWord

unbound

Adjust the current selection to include the next word using ForwardWord.

SelectNextWord

<Shift+Ctrl+Right Arrow>

Adjust the current selection to include the next word using NextWord.

SelectShellForwardWord

unbound

Adjust the current selection to include the next word using ShellForwardWord.

SelectShellNextWord

unbound

Adjust the current selection to include the next word using ShellNextWord.

SelectShellBackwardWord

unbound

Adjust the current selection to include the previous word using ShellBackwardWord.

SelectBackwardsLine

<Shift+Home>

Adjust the current selection to include from the cursor to the start of the line.

SelectLine

<Shift+End>

Adjust the current selection to include from the cursor to the end of the line.

SelectAll

<Ctrl+A>

Select the entire line. Moves the cursor to the end of the line.

SelfInsert

<a> <b> ...

Insert the key entered.

Redo

<Ctrl+Y>

Redo an insertion or deletion that was undone by Undo.

Undo

<Ctrl+Z>

Undo a previous insertion or deletion.

I really love the command-line editing, and it makes things a whole lot faster. I just need to keep remembering the shortcuts, which is why I made the two tables above. I have them printed out on my desk so that I have easy reference. They are also in the About_PSReadLine Help topic that is part of the PSReadLine installation.

Here is the video that illustrates using PSReadLine command-line editing to speed up Tab completion in Windows PowerShell:

That is all there is to using PSReadLine on the Windows PowerShell console command line. PSReadLine week will continue tomorrow when I will talk about more cool stuff.

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 


Viewing all articles
Browse latest Browse all 3333

Trending Articles