Visual Studio 2013: Using Box Selection and Zero-Length Box Selection
This is a collection of about 4 or 5 tips I wrote on using Box Selection for my book a while back. I think a lot of folks don’t know all the great things you can do with with this feature because they just don’t think about it much.
What is Box Selection?
The traditional use of this feature is to simply have some text you want to get rid of. Let’s say I have multiple lines of code:
And I need to get rid of some extra text. I can simply hold my ALT key and my left mouse button to highlight a vertical selection:
(NOTE: You can also hold the ALT key and use your ARROW keys to get the same result)
Now I can just press the DELETE key to remove the extra text:
While this is the traditional use of Box Selection there is much more here. Let’s take a look at some other things you can do.
Zero-Length Box Selection
Let’s say I want to change “bubba” to “cool” in the example code. Normally I would have to go to each line and replace the entries individually. However, in Visual Studio 2010, we introduced a feature called Zero-Length Box Selection. To use it, just go to where you want to start changing multiple lines of code:
Now you can use ALT+UP (or DOWN) ARROW to make a Zero-Length Box Selection:
(NOTE: You could also use ALT+Left Mouse to do the same thing but it’s a lot harder)
This creates a multi-line cursor (the thin blue line) you can use just like you are editing a single line of code. In this example, just press the BACKSPACE key to begin changing this text:
As you can see, all the lines are changed at the same time! Continue to remove “bubba” and then replace it with “cool”:
Now you have a multi-line cursor you can use to modify several lines at the same time. I love using this feature!
Replacing Text with a Box Selection
There is actually a faster way we could have replaced “bubba” with “cool” than the example above. Let’s reset the code:
Now, using ALT+Left Mouse or ALT+ARROW keys, select all the “bubba” entries:
And just type the word “cool”:
The word “bubba” will be instantly deleted and you will get a multi-line cursor that allows you to put in the new text.
Replacing the Contents of One Box Selection with Another
Another thing you can do is replace one set of text with another. Let’s say we have some variables:
And we want to replace “Int16”, “Int32”, and “Int64” with just “int”. No problem! Just select all the “int” entries and copy (CTRL+C) them:
Now select the destination:
And paste (CTRL+V):
It will replace the old contents with the contents you copied earlier.
Paste a Single Selection into a Box Selection
You can also use a single selection to replace multiple entries. If I have the following sample code:
I can copy the word “double” (CTRL+C):
Select all the “int” entries:
And paste (CTRL+V):
It will replace every “int” with the word “double” for every line! Another one of my favorites.
As you can see there are a lot of things you can do with Box Selection and Zero-Length Box Selection. Give it a try!