Freigeben über


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:

5-16-2012 12-01-47 PM

 

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)

5-16-2012 12-05-35 PM

 

Now I can just press the DELETE key to remove the extra text:

5-16-2012 12-07-32 PM

 

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:

5-16-2012 12-14-25 PM

 

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)

5-16-2012 12-17-25 PM

 

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:

5-16-2012 12-19-36 PM

 

As you can see, all the lines are changed at the same time! Continue to remove “bubba” and then replace it with “cool”:

5-16-2012 12-21-27 PM

 

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:

5-16-2012 12-27-38 PM

 

Now, using ALT+Left Mouse or ALT+ARROW keys, select all the “bubba” entries:

5-16-2012 12-30-33 PM

 

And just type the word “cool”:

5-16-2012 12-31-26 PM

 

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:

5-16-2012 12-36-40 PM

 

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:

5-16-2012 12-40-07 PM

 

Now select the destination:

5-16-2012 12-41-15 PM

 

And paste (CTRL+V):

5-16-2012 12-42-11 PM

 

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:

5-16-2012 12-46-59 PM

 

I can copy the word “double” (CTRL+C):

5-16-2012 12-47-48 PM

 

Select all the “int” entries:

5-16-2012 12-51-04 PM

 

And paste (CTRL+V):

5-16-2012 12-51-47 PM

 

It will replace every “int” with the word “double” for every line! Another one of my favorites.

 

 

 

Finally

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!

Comments

  • Anonymous
    July 15, 2013
    Maybe you should design your guy yourself instead stealing screenshots from Mac's Textmate? :-)

  • Anonymous
    July 15, 2013
    Gui not Guy

  • Anonymous
    July 15, 2013
    Hey Lord :) Yeah...so Textmate is just a copy and enhancement of some older technologies and those are enhancements of even older technologies all the way back to VI if you really want to get down to it. I can't speak to the completeness of Textmate but the point of the Visual Studio editor isn't that we cram every feature imaginable into it but rather that we try to put those things that we think matter most to developers. If I want a "kitchen sink" text editor then I usually go with UltraEdit personally. Z

  • Anonymous
    July 15, 2013
    The comment has been removed

  • Anonymous
    July 15, 2013
    Ben :) I'm happy to report that Find/Replace within a selection has returned in VS2013! Z

  • Anonymous
    July 15, 2013
    The comment has been removed

  • Anonymous
    July 15, 2013
    Aytham, I've been doing this a long time but that is one feature I've never heard any developer ask for. What would be the use case for selecting part of a variable name where the case changes? Z

  • Anonymous
    July 16, 2013
    There is an extension that allows you to do that (select on CamelCase words instead of the entire variable).  And I use it quite frequently.

  • Anonymous
    July 18, 2013
    Link?

  • Anonymous
    July 18, 2013
    Hey Jason / Aytham :) Looks like the extension is the VSTricks one found here: visualstudiogallery.msdn.microsoft.com/721ceda0-1b36-415b-9c23-ffcbcc83e76e I still think this is an edge case but glad to see our rich ecosystem took care of it :) Z