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.
Leave a Reply