Windows Command Line Auto Completion

Having explained this to a number of people in as many days, I thought I'd share this tip for working with long paths on the command line.  The easist way to expalin this is to jump in feet first:

  • Open a command line ( <Windows Key> -R, CMD <Enter> is engrained in my hands at this point in my career)
  • At the prompt, repeatedly hit tab

You will be cycling through all of the files and directories in your current directory.  Now try this:

  • At a clean command prompt, type "dir \P" then start hitting tab. 

Now you'll be cycling through all the directories that start with "P" in the root of the current drive (in my case, it cycles through "Program Files" then Projects).  You'll also notice that for files with embedded spaces, quotes are automatically added.  This works for as many characters as you wish to type. After you cycle to the directory you want, type a backslash, then hit tab again and you'll cycle through the files and sub-directories in that directory, and so on.  On my current machine, I can type the following to open my machine.config:

notepad \win <tab> \mic <tab> \ <tab> \v1.1 <tab> \con <tab> \m <tab><enter>

This results in the following command line, which would take considerably more keystrokes  or mouse clicks:

notepad \WINDOWS\Microsoft.NET\Framework\v1.1.4322\config\machine.config

This works at just about any point in a command, as long as you are starting from a space.  It will also work on UNC paths staring at the share name (Assuming you can authenticate to that particular share):

dir \\fileserver\share\ <tab>

There may be a delay as CMD goes an enumerates the items on the share, but if you are trying to get to something deep down in some huge directory structure with really long path names, this helps a ton.

Though this has been in CMD for some time, I don't think it was enabled by default untill Windows XP which may explain why it isn't well known. If you're stuck on a Windows 2000 box (or, gasp! NT4), you can edit the registry to enable this (Standard registry disclaimer applies - don't do this unless you know what you're doing since you can render a box un-bootable with sufficient registry tweaking.): 

Set HKCU\Software\Microsoft\Command Processor\CompetionChar to 9

KB244407 covers this in detail.

I used to hate the command line becuase I'm lazy and didn't want to type out huge paths (which I would end up misspelling).  No longer!