So this week I decided I’d do a Little Wonder of a different kind and focus on an underused IDE improvement: Visual Studio’s Box Selection capability. This is a handy feature that many people still don’t realize was made available in Visual Studio 2010 (and beyond). True, there have been other editors in the past with this capability, but now that it’s fully part of Visual Studio we can enjoy it’s goodness from within our own IDE. So, for those of you who don’t know what box selection is and what it allows you to do, read on! Sometimes, we want to select beyond the horizontal… The problem with traditional text selection in many editors is that it is horizontally oriented. Sure, you can select multiple rows, but if you do you will pull in the entire row (at least for the middle rows). Under the old selection scheme, if you wanted to select a portion of text from each row (a “box” of text) you were out of luck. Box selection rectifies this by allowing you to select a box of text that bounded by a selection rectangle that you can grow horizontally or vertically. So let’s think a situation that could occur where this comes in handy. Let’s say, for instance, that we are defining an enum in our code that we want to be able to translate into some string values (possibly to be stored in a database, output to screen, etc.). Perhaps such an enum would look like this: 1: public enum OrderType
2: {
3: Buy, // buy shares of a commodity
4: Sell, // sell shares of a commodity
5: Exchange, // exchange one commodity for another
6: Cancel, // cancel an order for a commodity
7: }
8:
Now, let’s say we are in the process of creating a Dictionary<K,V> to translate our OrderType:
1: var translator = new Dictionary<OrderType, string>
2: {
3: // do I really want to retype all this???
4: };
Yes the example above is contrived so that we will pull some garbage if we do a multi-line select. I could select the lines above using the traditional multi-line selection:
And then paste them into the translator code, which would result in this:
1: var translator = new Dictionary<OrderType, string>
2: {
3: Buy, // buy shares of a commodity
4: Sell, // sell shares of a commodity
5: Exchange, // exchange one commodity for another
6: Cancel, // cancel an order for a commodity
7: };
But I have a lot of junk there, sure I can manually clear it out, or use some search and replace magic, but if this were hundreds of lines instead of just a few that would quickly become cumbersome.
The Box Selection
Now that we have the ability to create box selections, we can select the box of text to delete! Most of us are familiar with the fact we can drag the mouse (or hold [Shift] and use the arrow keys) to create a selection that can span multiple rows:
Box selection, however, actually allows us to select a box instead of the typical horizontal lines:
Then we can press the [delete] key and the pesky comments are all gone! You can do this either by holding down [Alt] while you select with your mouse, or by holding down [Alt+Shift] and using the arrow keys on the keyboard to grow the box horizontally or vertically.
So now we have:
1: var translator = new Dictionary<OrderType, string>
2: {
3: Buy,
4: Sell,
5: Exchange,
6: Cancel,
7: };
Which is closer, but we still need an opening curly, the string to translate to, and the closing curly and comma. Fortunately, again, this is easy with box selections due to the fact box selection can even work for a zero-width selection! That is, hold down [Alt] and either drag down with no width, or hold down [Alt+Shift] and arrow down and you will define a selection range with no width, essentially, a vertical line selection:
Notice the faint selection line on the right? So why is this useful? Well, just like with any selected range, we can type and it will replace the selection. What does this mean for box selections? It means that we can insert the same text all the way down on each line! If we have the same selection above, and type a curly and a space, we’d get:
Imagine doing this over hundreds of lines and think of what a time saver it could be! Now make a zero-width selection on the other side:
And type a curly and a comma, and we’d get:
So close! Now finally, imagine we’ve already defined these strings somewhere and want to paste them in:
1: const private string BuyText = "Buy Shares";
2: const private string SellText = "Sell Shares";
3: const private string ExchangeText = "Exchange";
4: const private string CancelText = "Cancel";
We can, again, use our box selection to pull out the constant names:
And clicking copy (or [CTRL+C]) and then selecting a range to paste into:
And finally clicking paste (or [CTRL+V]) to get the final result:
1: var translator = new Dictionary<OrderType, string>
2: {
3: { Buy, BuyText },
4: { Sell, SellText },
5: { Exchange, ExchangeText },
6: { Cancel, CancelText },
7: };
Sure, this was a contrived example, but I’m sure you’ll agree that it adds myriad possibilities of new ways to copy and paste vertical selections, as well as inserting text across a vertical slice.
Summary:
While box selection has been around in other editors, we finally get to experience it in VS2010 and beyond. It is extremely handy for selecting columns of information for cutting, copying, and pasting. In addition, it allows you to create a zero-width vertical insertion point that can be used to enter the same text across multiple rows.
Imagine the time you can save adding repetitive code across multiple lines! Try it, the more you use it, the more you’ll love it!
Technorati Tags: C#,CSharp,.NET,Visual Studio,Little Wonders,Box Selection