Wednesday, February 15, 2006

You thought you knew everything about cmd.exe!

Last month, Michael Kaplan explained that after all this time, he thought he knew everything there was to know about the MessageBox Win32 API yet managed to find something new to him (and to me as well BTW ;-).

And after all this time, I thought I knew everything one might want to know about the good old command prompt, cmd.exe:

Of course I know that Windows XP's command prompt accepts document names in addition to exe command names. e.g.: you can type services.msc to open the Services applet. Or you can simply type the name of a Word document to open it.
(I say XP's command prompt because IIRC it wasn't the case in previous versions).

I knew about how to enable auto-complete of filenames in Windows 2000 before it became a default setting in XP.

OK, for sure I'm no expert in batch files but at least I know my limits in the area.

A (not so) hidden command history list

But last week, I discovered that I'm not as knowledgeable as I thought. Will you believe the unbearable? Yes, a cmd.exe feature I had never seen before. I was switching between Visual Studio and a couple of command prompts. At some moment, I hit F7 to recompile while the focus was still in one of the command prompts.



Ho! F7 triggers a pop-up window with the command history. Boy, I just can't believe how I have been able to never see that before! The funny thing is that I talked to a couple other developers about it and none of them knew about F7 either.

Auto-complete recent commands

OK, let's try the other function keys to see if there's another handy goodie hiding in there. F8 is the only other interesting one I found: type the start of a command, then F8 repeatedly: it will auto-complete your command with the ones in your command history.

Console window title



While on the subject of less known features, the title command is another interesting one: it sets the title of the console window. This is particularly interesting when you keep several command prompts opened simultaneously : You can immediately identify each one in the task bar instead of having to go through each of these anonymous "c:\windows\sys..." task bar buttons.



And if you are interested in Internet Explorer keyboard shortcuts, Jeff Atwood recently posted a summary.

8 Comments:

At 12:53 AM, Anonymous Daniel Lohmann said...

He, he, the good old cmd.exe is a surprising tool and it brings all its whistles and bells only step-by-step to the public. Almost nobody knows that all these things even worked in Windows NT 3.1! Yes, tab completion, history lists and so on have been available since the very first day of Windows NT.

I'd just like to add some more more or less known secrets:

Fn keystrokes:
-Serge already mentioned F7 to get a command history list. Every commands in this history is numbered. By pressing F9+[number] you can execute one of them again without having to go through the list.

-F4 opens a box where you can enter a character. The part of the command line right of the curser is deleted up to this caracter.

-F2 works similar, but simply jumps to the entered character. (The function describes itself as "copy" which is a bit confusing)

-F6 prints out an EoF character (Ctrl+Z). This can be used to terminate a console window redirection (e.g. started with "type con > newfile" to enter a few lines into a text file)

-F5 and F8 can be used to walk through the command history. (Not that exciting - unless you find yourself in front of a keyboard without cursor keys :-)

Some more "meta"-commands:
Besides the "title" command many other are available to change the appearance of a console window. Most usful are (IMHO):

-the "color" command. I usually have a cmd.exe shell running as Administrator on my desktop. A simple "color cf" paints it red, preventing me from accidentally using it for non-Admin tasks.

- the "mode" command. Can be used to change the size of the console window without having to go through the menus. Example: "mode con cols=100 lines=60"

AutoRun:
It is possible to execute some commands whenever a new console window is started. CMD.exe looks under "[HKLM|HKCU]\Software\Microsoft\Command Processor" for a string value named "AutoRun" and executes whatever it finds there. This is particulary useful to distinguish different user accounts. I put, for instance, the "color cf" into the AutoRun string for user Administrator. Another idea is to put a "title %USERNAME%" there.

And AutoRun, at least, is really a new XP feature :-)

Have fun!

Daniel

 
At 11:53 PM, Anonymous Serge said...

Hey Daniel!
Glad to 'see' you here.

Wow! Do you mind if I call you Danielpedia :-)

 
At 9:53 PM, Anonymous Anonymous said...

Hi,

The F7 was really good one

Patricia Govezensky

 
At 12:25 AM, Anonymous ofer said...

Great!!!
i want to add another tip (although its works in most windows applications).
when you press ctrl + left/right arrow the curser jumps to the next/previous word.
when you press ctrl + home/end all the charecters from the curser to the begining/end of the row are deleted.

 
At 8:59 AM, Anonymous Serge said...

Ofer,

That's a great tip. Of course people know these shortcuts in regular Windows apps. But I bet most never realized it works in consoles as well ;-)

The thing I'd love with Ctrl+left/right would be that it stops at each backslash as well. It would make it easier to manipulate paths.

 
At 6:19 PM, Anonymous Peter Louwen said...

All this is old news for
anyone who has ever used Doskey (which was available
in DOS since 1800 or so).

The F6 trick dates back even further.

If only they made function key redefinition a la ANSI.SYS available again...

 
At 6:53 PM, Blogger Serge Wautier said...

Oh really? I thought I missed it during 10 years. And you're telling me it's been at my fingertips for 20 years... Ouch! I'm even more ignorant than I thought :-)

 
At 11:18 AM, Anonymous Anonymous said...

Hi,

I'm in the opposite situation... After executing a command, the history list pop up and there is nothing else i can do than re executing the same command or closing de whole console... The problem occure on one of our server and is very annoying. hitting F7 doesn't make the history list disepear.

Any solution?

Nico

 

Post a Comment

<< Home