When I first saw the box selection capabilities in Visual Studio 2010 I thought “that’s kind of neat but I’ll probably never use it” and promptly moved on. I couldn’t have been more mistaken. In fact, nearly two years later, box selection has become one of those features that I use almost daily. What surprises me now though is how many developers I run into that still don’t know about them.
Box selections let us quickly make the same change to multiple lines simultaneously. Creating them is easy – just hold shift+alt and use the arrow keys or hold the alt key while left drag the mouse to define a rectangle. If you just want to insert the same text onto multiple lines you can define a zero-length box by expanding the box vertically in either direction.
So what makes box selections so useful? Some of the things I find them most useful for are changing modifiers and making local variables implicitly typed. To illustrate, let’s take a look at a few non-virtual properties that we’d like to make virtual.
Making these properties virtual without a box selection certainly isn’t difficult but it’s definitely tedious. A box selection lets us make them all virtual at the same time so we can get on with the task at hand. The thin blue line immediately following the public modifier on each property identifies the zero-length box that serves as the point where we’ll insert in the virtual modifier.
To insert the virtual modifier we just need to type (or paste) “virtual.” Here you can see that each property is now virtual and the zero-length box has moved to the end of the inserted text. What if we decide later though that these properties shouldn’t be virtual after all?
We can use box selections to remove the virtual modifier from each property just as easily. In the example to the left we see a box selection highlighting the virtual modifier on each line.
To remove the text we can simply delete it. This will leave us with a zero-length box where the virtual modifiers used to be. We can then simply click or arrow away to clear the box selection.
Box selections can go a long way toward increasing your productivity by reducing some of the more tedious aspects of programming. The few seconds they save here and there can really add up over the course of a day. More importantly though, that time can be spent on the real problems we’re trying to solve.
Further Reading
How to: Select and Change Text
Tagged: Box Selection, Box Selections, Visual Studio, Visual Studio 2010, VS2010
