Dont Forget Block Selection Using Alt in Visual Studio

Date Published: 18 November 2012

Dont Forget Block Selection Using Alt in Visual Studio

So do you ever find yourself wanting to try out some code you found on the Internet (via Copy Paste Programming, but in a spike or test project, naturally!), and unfortunately when you copy the code it includes a bunch of line numbers? Like the code from this sample on testing Entity Framework stuff?

  1: [TestClass]
  2: public class TestRunDatabaseCreator {
  3:  4:   public const string DATABASE_NAME_TEMPLATE = "CPT_CDS_{0}";

Of course, some code highlighting tools make this a little easier for you, and Scott Hanselman shows off some options. But in this case, the post was made some time ago, so I don’t think we can rely on the author to change how they’ve chosen to post the code itself. There’s always the manual option, which I know I’ve done plenty of times. That is, type del del del del del downarrow, del del del del del downarrow, … repeat. That gets old fast – there should be a better way.

Fortunately, there is, and it’s been around for a long time. I was just reminded of it today, and thought I’d share for those of you who don’t know about it. Just use the block selection option in Visual Studio, which you get by holding down Alt while selecting with the mouse. Take some code like this:

Highlight with the mouse while holding alt:

And hit delete. Problem solved.

Share this if it helped you, and happy coding!

Steve Smith

About Ardalis

Software Architect

Steve is an experienced software architect and trainer, focusing on code quality and Domain-Driven Design with .NET.