Random geekery since 2005.

Husband, father, programmer, speaker, writer, blogger, podcaster, collector, traveler, golfer.

Today is the second day of Diduary. You can see links to all of the published articles in the series here.

You may or may not have been aware that in Visual Studio, you have the ability to select blocks of text. I’m not talking about lines of text, or certain characters…you can do that in nearly any text editor.

Block select is that thing that you always wish for when you’re working on an HTML or XML document (or really any formatted text), and perhaps you want to delete the same attribute from 10 different lines. Here’s an example:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="BlockReplace._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
<
title>Block Replace Example</title>
<
style type="text/css">
.replacement{
background-color:#FFFFFF;
}
</style>
</
head>
<
body>
<
form id="form1" runat="server">
<
div class="replaceme" id="div1"></div>
<
div class="replaceme" id="div2"></div>
<
div class="replaceme" id="div3"></div>
<
div class="replaceme" id="div4"></div>
<
div class="replaceme" id="div5"></div>
<
div class="replaceme" id="div6"></div>
<
div class="replaceme" id="div7"></div>
<
div class="replaceme" id="div8"></div>
<
div class="replaceme" id="div9"></div>
</
form>
</
body>
</
html>

You can see that I have misspelled the class name on each of my <div> tags. While some of you may argue that I could use find & replace for this, if I has simply used the wrong class name, we’d have a tougher time with that.

Instead, in Visual Studion 2010, you can now use Block Replace. To do this, highlight the text as you normally would, but hold down Shift + Alt while you do it. You’ll see that you get a columnar highlighting tool, allowing you to select the “replaceme” in each line, and nothing else. It would look like this:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="BlockReplace._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
<
title>Block Replace Example</title>
<
style type="text/css">
.replacement{
background-color:#FFFFFF;
}
</style>
</
head>
<
body>
<
form id="form1" runat="server">
<
div class="replaceme" id="div1"></div>
<
div class="replaceme" id="div2"></div>
<
div class="replaceme" id="div3"></div>
<
div class="replaceme" id="div4"></div>
<
div class="replaceme" id="div5"></div>
<
div class="replaceme" id="div6"></div>
<
div class="replaceme" id="div7"></div>
<
div class="replaceme" id="div8"></div>
<
div class="replaceme" id="div9"></div>
</
form>
</
body>
</
html>

At this point, you can simply start typing, and Visual Studio 2010 will replace all of the characters, in EACH LINE, with the characters you are typing. It’s a little surprising the first time you see multiple lines being edited at the same time, but it will also potentially save you some time fixing those mistakes.

Tags

One response to “The 2nd of Diduary: Did you know about “Block Replace” in Visual Studio 2010?”

  1. Brendan Enrick Avatar
    Brendan Enrick

    I did not know that they added in replace. Not I don't have to switch to different text editors to do this kind of stuff.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: