Hacking Visual Studio

Author's note: Following are some of the hacks from my book that stand out to me for different reasons. "Master the Command Window" (Hack #46) is one of the most time-saving and keyboard-friendly parts of Visual Studio. "Create Comments Faster" (Hack #69) is one of my favorite hacks since it covers one of my all-time favorite add-ins and is written by Roland Weigelt, creator of the add-in. "Run Unit Tests Inside Visual Studio" (Hack #93) covers one of my other top picks for add-ins, TestDriven.NET. It's one of the only add-ins that can really affect the way that you go about writing code. "Hack the Project and Solution Files" (Hack #4) is an interesting hack because it covers one of the undocumented parts of Visual Studio. I also selected "Refactor Your Code" (Hack #14) because it covers what I think is hands-down the best new feature in Visual Studio 2005, the Refactor menu.

Master the Command Window

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.

Command Window Basics

So 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

This might not seem like much, but if you have a dozen projects with
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.

Table 6-1. Useful Visual Studio commands and aliases

Command

Alias

Description

File.OpenFile

open

Opens the file specified as a parameter

File.NewFile

nf

Creates and opens a new file

File.NewProject

np

Creates and opens a new project

File.AddNewProject

addProj

Creates and adds a new project to the current solution

File.SaveAll

SaveAll

Saves all the currently open files

File.Close

close

Closes the selected file

View.FullScreen

FullScreen

Switches to full-screen mode in the IDE

View.Toolbox

toolbox

Shows the toolbox window

View.PropertiesWindow

props

Shows the properties window

Edit.GotoBrace

GotoBrace

Skips to the corresponding brace (e.g., the closing brace of an if
statement)

Edit.SelectAll

Selects all the text in the current document

Edit.Undo

undo

Equivalent to Edit → Undo

Edit.Redo

redo

Equivalent to Edit → Redo

Edit.NextBookmark

NextBook

Jumps to the next bookmark

Edit.PreviousBookmark

PrevBook

Jumps back to the previous bookmark

Edit.CollapsetoDefinitions

StopOutlining

Collapses all collapsible section of code (classes, regions, etc.)

Build.BuildSolution

build

Builds the current solution

Debug.Print

?

Shows the value of the variable passed in as a parameter

Debug.QuickWatch

??

Displays the quick watch dialog for the variables passed in as a
parameter

Tools.Alias

alias

Lists all currently defined aliases or defines a new one

When in doubt, you can sometimes fall back on old
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 → Options
→ Keyboard screen [Hack #24] .

Debugging with the Command Window

Perhaps 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:

>? i
0

In 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 =2
2

As 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:

>immedi
0

To 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.

Create Window Aliases

Aliases 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.SelectAll

You 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.cs
>openClass1

This 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

View and Edit Command Window Aliases

While 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 the Alias Manager, you can create, edit, and delete aliases
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.