Box Selection and Multi-Line Editing with VS 2010

This is the twenty-second in a series of blog posts I’m doing on the VS 2010 and .NET 4 release.

I’ve already covered some of the code editor improvements in the VS 2010 release.  In particular, I’ve blogged about the Code Intellisense Improvements, new Code Searching and Navigating Features, HTML, ASP.NET and JavaScript Snippet Support, and improved JavaScript Intellisense.  Today’s blog post covers a small, but nice, editor improvement with VS 2010 – the ability to use “Box Selection” when performing multi-line editing.  This can eliminate keystrokes and enables some slick editing scenarios.

[In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu]

Box Selection

Box selection is a feature that has been in Visual Studio for awhile (although not many people knew about it).  It allows you to select a rectangular region of text within the code editor by holding down the Alt key while selecting the text region with the mouse.  With VS 2008 you could then copy or delete the selected text.

VS 2010 now enables several more capabilities with box selection including:

  • Text Insertion: Typing with box selection now allows you to insert new text into every selected line
  • Paste/Replace: You can now paste the contents of one box selection into another and have the content flow correctly
  • Zero-Length Boxes: You can now make a vertical selection zero characters wide to create a multi-line insert point for new or copied text

These capabilities can be very useful in a variety of scenarios.  Some example scenarios: change access modifiers (private->public), adding comments to multiple lines, setting fields, or grouping multiple statements together.

Great 3 Minute Box-Selection Video Demo

Brittany Behrens from the Visual Studio Editor Team has an excellent 3 minute video that shows off a few cool VS 2010 multi-line code editing scenarios with box selection (note: enable your speakers as there is audio during the demo):

 

Watch it to learn a few ways you can use this new box selection capability to optimize your typing in VS 2010 even further:

Hope this helps,

Scott

P.S. You can learn more about the VS Editor by following the Visual Studio Team Blog or by following @VSEditor on Twitter.

Published Monday, April 26, 2010 5:53 PM by ScottGu

Comments

# re: Box Selection and Multi-Line Editing with VS 2010

Monday, April 26, 2010 9:07 PM by Nicholas Blumhardt

LOVE the box selection improvements! Can't imagine a time when we lived without these :)

# re: Box Selection and Multi-Line Editing with VS 2010

Monday, April 26, 2010 9:08 PM by wisecarver

Very handy feature...Thanks  ;-)

# re: Box Selection and Multi-Line Editing with VS 2010

Monday, April 26, 2010 9:28 PM by Damian Edwards

This feature is particularly useful when restructuring HTML as it often includes many repetitive blocks that line up in the editor vertically. E.g. changing

<table>

   <tr>

       <td><a href="#"><img src="#" alt="Some link" />Link 1</a></td>

       <td><a href="#"><img src="#" alt="Some link" />Link 2</a></td>

       <td><a href="#"><img src="#" alt="Some link" />Link 3</a></td>

   </tr>

</table>

to

<ul>

   <li><a href="#" title="Some Link"><span>Link 1</span></a></li>

   <li><a href="#" title="Some Link"><span>Link 2</span></a></li>

   <li><a href="#" title="Some Link"><span>Link 3</span></a></li>

</ul>

# re: Box Selection and Multi-Line Editing with VS 2010

Monday, April 26, 2010 9:52 PM by Snahider

Nice, cute and useful feature. Thanks =)

# re: Box Selection and Multi-Line Editing with VS 2010

Monday, April 26, 2010 9:54 PM by rajbk

You can also move/copy a box selection by dragging/ctrl dragging your cursor.

Raj

# re: Box Selection and Multi-Line Editing with VS 2010

Monday, April 26, 2010 10:02 PM by Keith

This is a bit of a non feature.  While it "looks" cool, its not really needed and you can pretty much get the same thing with find/replace in selection ( and we have had box selection for ages).  In fact you are way better of becoming a find/replace ninja.  It would of been nicer if there was awesome improvements to find/replace  to make it  find/transform.   which is what people generally want.

prime example being in the Video shown, she pasted all the pens with ;,  and then had to manually edit them out.   A Semantic paste would omit the ';'!   There's all kinds of cool things like this that could be done...

but no....  Microsoft and the visual studio team are going WOO HOO! look at this cool feature we have just added that many other editors have had for years WOO HOO, look look!   how embarassing.....

Tell us when you actually have something cool to look at in the editor!

(  Because there are heaps of other cool stuff in Visual Studio 2010, but the editor and a number of the standard dialogs still suck and haven't really changed much...   look at the keybinding window for instance.   But woo hoo, I can zoom my font in and out, woo hoo, just what I didn't need.  Sure maybe I'll use it giving a demo.... but would of traded that in a heart beat for a billion other improvements)  

*sigh*  ok, I'll stop bleating.  But hey, maybe Microsoft can use all their smarts to make code editing amazing? that would be cool.  

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 1:27 AM by Emil Ingerslev

This is a great feature, but I think multiple line edit on different column positions (not on a straight vertecally line) is needed to make it truly useful. But great with more of these simple but delicious editor features!

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 1:55 AM by Tim

I'm sorry UltraEdit, this was why I kept you around... :~(

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 2:21 AM by Leonid Volnitsky

Finaly VS caught up with VI.  This was introduced in VIM about 15 years ago.

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 2:56 AM by Kobi

Very nice and handy. Thanks

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 5:02 AM by zire

Good video & useful information , thanks :)

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 7:31 AM by Tom

This is a nice feature.  I've been using box editing in VS for many years and it's cool to see it not only preserved in the new editor, but extended in a useful way.

Keith, want some cheese?

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 10:50 AM by Anonymous

@Leonid Volnitsky: Next thing you know, VS will copy VI even more and you will be able to close the files with the all-time-favourite command: "Escape-colon-q" - ESC:q

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 10:51 AM by Edwin

Scott, thanks for posting this video. I think there should be more and more feature videos in this format. It's concise, straight to the point and Brittany Behrens's voice is very sweet and gentle. I'm looking forward  to more great videos like this.

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 12:39 PM by Alex

In Notepad++ it is possible to move the current line up and down with CTRL+ALT+UP/Down.

Is something like this possible in VS 2010?

# re: Box Selection and Multi-Line Editing with VS 2010

Tuesday, April 27, 2010 4:39 PM by rabeehabla

Hi thanks for the great post,

I have question related to Silverlight control inheritance, it can be solved by many simple ways, one of them is to prevent the designer custom tool "MSBuild:MarkupCompilePass1", from specifying in the designer generated partial class, the inheritance to User control...

   public partial class MyControl : System.Windows.Controls.UserControl {

...

}

That way in the control code class we simply set the parent of the control, without having different parent classes,

each time the designer generates it code..

Regards

# re: Box Selection and Multi-Line Editing with VS 2010

Wednesday, April 28, 2010 10:16 AM by TaylorMichaelL

Box selection has always been a cool feature that came in real handy when you're block copying and editing lines of code (like converting #define lines into enums).  The new ability to change all the lines to the same thing is going to make this process even easier.  Great addition.

# re: Box Selection and Multi-Line Editing with VS 2010

Wednesday, April 28, 2010 11:54 AM by Sam

this looks good, but i wonder if its possible to do it with refactoring as well?

not much advantage gained if you can easily modify groups of variables, but then have to go through all your code anyway.

# re: Box Selection and Multi-Line Editing with VS 2010

Wednesday, April 28, 2010 4:11 PM by Anvar

Cool feature. Can be useful in repetitive chunks of code.

# re: Box Selection and Multi-Line Editing with VS 2010

Thursday, April 29, 2010 10:08 PM by shou

This is very cool and useful feature.

# re: Box Selection and Multi-Line Editing with VS 2010

Saturday, May 1, 2010 10:14 PM by Milan

Very nice.

Just spotted that it doesn't give smart tag for updating references to renamed objects once she changed prefix for those fields.

--- Milan

# re: Box Selection and Multi-Line Editing with VS 2010

Monday, May 10, 2010 10:35 PM by Ashley

Box editing is good, but the Brief editor key mapping was betting. Rather than ditch the Brief keyboard, MS should include allow these keyboard mappings to be included in the standard mapping, rather than replacing the standard key mapping, and allow everyone to see how quick they are to use.

# re: Box Selection and Multi-Line Editing with VS 2010

Sunday, June 6, 2010 3:38 AM by Chris Taylor

Scott, an important enhancement to box selection that was introduced in VS2010 that I seldom see mentioned is that it now supports selection via the keyboard. For me I prefer the keyboard for most actions especially selection, now in VS2010 Shift+Alt+[Up,Down,Left,Right] will create a box selection. This is especially nice for the zero width insertion point selection.