Section 2.3. Work with the Command Line


2.3. Work with the Command Line

Because cmd.exe runs in the Windows console subsystem, it inherits a number of features useful for command-line editing . The ability to edit the current command, as well as recall previous ones, proves to be a useful tool when working in an interactive command shell (and this feature continues to work in MSH). In this short section, we'll look at some of the standard editing keystrokes, as well as changes in the history buffer, that MSH offers.

The Legacy of DOSKEY.EXE

There was a time when DOSKEY was a vital part of any startup script on an interactive system. Building on the simple "recall last command" behavior, DOSKEY extended the command line with rich history, recall, and editing features. Instead of just recalling the last line, it suddenly became possible to recall any of the previous instructions. Fixing a typo in a previous command was also made easier, with the ability to move left and right along the input and inserting, deleting, or changing characters as needed.

In recent operating systems, the Windows console continues to support the same set of rich command editing and recall features with built-in functionality. MSH inherits this wholesalethe keystrokes and function keys retain their special powers throughout an MSH session.

Although DOSKEY remains a complement to cmd.exe, its other functionality deals with macros, a feature that is wholly overshadowed by the rich scripts and functions available in MSH.


2.3.1. How Do I Do That?

Command-line editing makes life a lot easier when entering commands in interactive mode. Suppose you've missed a parameter; no problem, just use the left arrow key to move back to the correct position and insert it. Need to fix a typo? Same idea. Use the cursor key to move to the correct position and then use Delete or Backspace to make any necessary changes. Other familiar shortcuts, such as Ctrl-left arrow, Ctrl-right arrow, Home, and End, will skip between words and go to the start and end of the command line, respectively.

When in interactive mode, every command entered is stored in what is known as the history buffer. The idea of a history buffer isn't new and is available in most command-line interpreters available today. As with most shells, the up and down arrows can be used to navigate through the history buffer, recalling previously executed commands and bringing them to the current command line. Also present is the Page Up/Page Down mapping to recall the first and last entries in the history buffer, respectively. Function keys also play their part, from F3 copying the remainder of the previous command into the current command line to F7 displaying a list of all history items in a pop-up box. Try them out, and you'll find that all of the command-prompt keystrokes continue to behave as expected.

MSH takes the history buffer one step further. The get-history cmdlet lists the content of the history buffer by placing HistoryInfo objects into the pipeline. From there it's only a short step to start using some of the familiar cmdlets for sorting and filtering, really making the history work for you.

As an example, let's take the case where we executed a command some time earlier and would now like to rerun it. Instead of browsing through the history by hand, let's set where-object to display the likely candidates:

     MSH D:\MshScripts> get-history | where-object {$_.CommandLine -like "get-*"}       Id CommandLine       -- -----------       75 get-process       77 get-childitem -Recurse -Filter *.bak | sort-object Name       82 get-location      102 get-location

There it is: history item 77. Now we can use the invoke-history cmdlet to recall and execute the command line immediately:

     MSH D:\MshScripts> invoke-history 77     get-childitem -Recurse -Filter *.bak | sort-object Name         Directory: FileSystem::D:\MshScripts     ...

There's one other useful trick to rely on when using interactive mode. So far, all of the interactive mode examples we've encountered have fit on a single line. However, when working with many instructions, it will often be more convenient to put different instructions on each line. To stop MSH from trying to run the first line as soon as Enter is pressed, we use the escape character (') at the end of the first line. This indicates that we're not quite ready yet and MSH should wait for all lines to be entered, followed by one last empty line, before running the command:

     MSH D:\MshScripts> get-childitem '     | where { $_.Extension -eq ".txt" }     | sort Name     | format-table Name, Extension     # equivalent to     # get-childitem | where { $_.Extension -eq ".txt" } | sort Name | format-table Name,  Extension

In many cases, MSH is able to see that a continuation is neededfor example, if a line ends with a pipe symbol or inside a function definition. In these cases, MSH switches to the line continuation behavior until it sees an empty line.

We'll look at the other uses of the special ' character in Chapter 4.

2.3.2. What About...

...Increasing the size of the history buffer? By default, the history buffer stores only the last 64 commands, but this is easily changed. The limit is stored in an MSH variable called $MaximumHistoryCount. We'll look at variables more in the next chapter, but for now we can expand the buffer size by changing this value to a larger number:

     MSH D:\MshScripts> $MaximumHistoryCount=1024

...Dumping the last few commands into a script file? If you've just entered a series of commands by hand but would like to keep the commands for reuse at a later date, it's easy to format the output of get-history for script generation. Let's say we'd like to store the last six commands into a script for future use. The Count parameter takes a number value corresponding to the number of entries to return, counting backward from the most recent:

     MSH D:\MshScripts> get-history -Count 6 | format-table -HideTableHeader CommandLine     cd \mshscripts\tutorials     delete *.bak     cd \mshscripts\samples     delete *.bak     cd \mshscripts\walkthroughs     delete *.bak

It's possible to redirect this output to a script by adding >D:\MshScripts\remove-backups.msh (or something similar) to the end of a pipeline. If the commands you're trying to save are scattered through the history, it might be easier to call get-history without any parameters and cut and paste the relevant commands in a text editor.

There is one additional feature that MSH brings to the command-line editing experience: rich tab completion. From an empty command line, pressing the Tab key will insert the filename of the first file in the current directory onto the command line. Pressing it again will replace it with the next file, and so on, just like cmd.exe. However, MSH takes tab completion a step further and also offers completion of cmdlet names. Typing get-pro[Tab] will complete the get-process, while get-[Tab] can be used to cycle through all get-* cmdlets.

2.3.3. Where Can I Learn More?

The full list of DOSKEY keystrokes is supplied in the Windows XP documentation at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/doskey.mspx. Although the DOSKEY syntax isn't used in MSH (it's replaced by aliases and functions), the keyboard shortcuts remain relevant.

More information on the get-history and invoke-history cmdlets is available through get-help.

By this point, we've seen a number of ways to customize the environment. Next, we'll take a look at the profile script and see how we can make these customizations available every time the shell is started.




Monad Jumpstart
Monad Jumpstart
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 117

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net