Hack 46. Master the Command Windowvisual, command-line junkies don't need to fear it. Visual Studio has hundreds of menus, windows, and dialog boxes. This you are probably aware of; what you may not be aware of is that you can avoid using all of these and use the command line inside the command window instead. This hack looks at some of the different commands available to you, how to use existing aliases, and how to create and manage your own aliases. 6.4.1. Command Window BasicsSo why would you want to use a command window when you could just use some part of the IDE? Using the command window is sometimes faster than using the IDE, and when you are writing code with both of your hands on the keyboard, it is often easier and faster to type a command than to reach for the mouse. Many people who are used to the good old days of the command prompt find themselves right at home with the command window, but whether or not you are one of these people, I encourage you to explore its functionality.The main keystroke to remember is Ctrl-Alt-A (View.CommandWindow); this is the shortcut to open the command window.The open command is the first command I am going to cover. Using the open command, you can open any file in either the filesystem or the current solution. (I think the real added value here is opening a file in the current solution, since the command window provides IntelliSense.) Figure 6-2 shows the command window and the IntelliSense available for the open command. Figure 6-2. Command Window open command![]() hundreds of files, this becomes much faster than digging through the Solution Explorer with your mouse.You can use any Visual Studio command directly through the command window (through this book, whenever we mention a keyboard shortcut, we've also been mentioning the command). Any command can be used through the command window.Twenty of the most useful commands are shown in Table 6-1. You can either type the full command or use the alias.
MS-DOS command prompt habits: for instance, the command cls will clear the command window. You can find a complete list of commands in the Tools 6.4.2. Debugging with the Command WindowPerhaps the most useful function of the command window is the ability to use it to view the values of variables during the debugging process. You can use a number of different commands during the debugging process to read and set the values of variables. You can simply type a question mark, a space, and then the name of a variable, and when you press Enter, the value of that variable will be printed to the screen. Here is an example of this command: >? iIn this example, the value of the variable i is zero. You can also set the value of a variable through the command window by using the question mark, a space, the name of the variable, and then an equals sign and the value that you want to set the variable to. Here is an example of setting the value of a variable through the command window: >? i =2As an alternative to using the question mark, you can also set the command window to immediate mode; this turns the command window into an immediate window and you no longer need to use a question mark. You can set the command window to immediate mode by typing in the command immed. Following is a transcript of using the command window in immediate mode: >immedTo switch the command window back into command mode, you simply need to type in any command prefixed with >. For instance, you could type >cmd and the window would switch back to command mode.Another benefit to viewing the value of a variable in the command window is that it is easy to copy the value of that variable out of the command window. If you are working with a large string of XML, it might be valuable to get the value of that XML document and then copy it to your favorite XML application to view the data in a friendlier format. 6.4.3. Create Window AliasesAliases are a way of creating a custom command that is short for a longer command. The Open command shown earlier is actually an alias that Visual Studio defines for the more verbose command File.OpenFile. To create new aliases you simply need to type alias, the name of the alias, then the command that you want to execute for this alias. Here is an example of creating an alias for the Edit.SelectAll command: >alias selectall Edit.SelectAllYou can now select all the text on the screen by calling the alias selectall. You can also create aliases that include a parameter for a command. You can create a command called openClass1 that calls the File.OpenFile command and also specifies which file to open. Here is an example of this command: >alias openClass1 File.OpenFile Class1.csThis way you can call the openClass1 command at any time to open the Class1.cs file. You can also remove aliases that you have already created by simply adding the /delete switch at the end of the alias command. Here is an example of how to remove the openClass1 alias: >alias openClass1 /delete 6.4.4. View and Edit Command Window AliasesWhile you can create and edit aliases directly in the command window, the VSTweak power toy provides an easy-to-use graphic interface for these command window aliases. Using this interface, you can add, edit, or delete command window aliases.The VSTweak power toy is one of the more useful power toys for Visual Studio and is the subject of a number of different hacks in this book. The VSTweak power toy [Hack #13] can be downloaded from http://workspaces.gotdotnet.com/vstweak. Figure 6-3 shows an example of this interface. Figure 6-3. The VSTweak Alias Manager![]() using a nice graphical interface. The Available Commands button will show all of the available commands that you can create aliases for. The View File button shows the aliases.ini file, which stores all of the command window aliases.The command window offers a lot of features that can be used to increase your productivity while working with Visual Studio, particularly when debugging. |

