Formatting Replace Output

 


This post applies to Visual Studio 2010. I’ll do another post on Visual Studio 2012.


I’ll show you how to control the output of Replace in Files. To see where this is going, here’s before and after pictures.


Here’s the view before the change. Note that I scrolled right to show the end of the line, and if the names of the directories were different lengths, this would not line up at all. This is about the best this will ever look:clip_image002


You control the output after the change. Here’s one possiblity:clip_image004


Personal Note: On rare occasions you get to find something that isn’t reported elsewhere, making you feel like you’ve found something no one else in the world knows – although obviously this isn’t a new continent and I’m not the very first person to know how to alter the output of Replace in Files. Still finding something that isn’t documented yet on the web is pretty exciting.


Find Format


I love Sara Ford and Zain Naboulsi. They’ve posted boatloads of tips on Visual Studio over the years and they’ve got a great tips and tricks book. One of the great features that Sara introduced us to in 2006 is formatting the output of Find in Files – you can find that post here .


As Sara explained, the output of the find results window is controlled by a registry key called “Find result format.” It’s a rather arcane syntax that creates a string output for each line, you can’t control the output by columns except with tabs, but you can create much more usable output than the default. I’ll let you check out Sara’s article for details.


Replace in Files


The Visual Studio Replace in Files feature is cool – you can control the scope of the replacement better than Quick Replace, you can control whether files are left open, and you get a list of all the changes made. That list of changes is a great feature. It’s so great that you might even want to use it if you are just making replacements in a single file.


You’ll find Replace in Files if you drop down the Replace combo box at the top of the Find dialog. You can select al of the standard scopes, as well as selecting specific folders by right clicking the three dots to the right of the combo box.


When you select Fnd All, Visual Studio lists of the changes that were made, in the Find Results window. If you don’t like the changes, and you left files open, you can undo all the changes with a single Ctl Z.


Replace Output Format


I was really disappointed that the Find result format did not affect the output of Replace in Files. The oversight surprised me, so I decided to take a shot at creating a registry key named “Replace result format.” I wasn’t optimistic, but voila’ it worked!!!


Note: You can screw up Visual Studio, or even your computer if you mess up your registry. Be careful out there.


To make this change, open RegEdit and find the key


HKEY_CURRENT_USER/Software/Microsoft/VisualStudio/10.0/Find


Add a string value named “Replace result format” (no quotes), modify, and add your preferred format.


Here’s a summary of the important format options for Find.


clip_image006


All these format options work in Replace as well. In Replace, and there are some additional options that don’t make much sense in Find.


clip_image008


In Sara’s post, she refers to $t, $T, and $s as matched values. This probably seems picky, but for clarity I want to point out that it’s not the matched value, but the replaced value that’s displayed. Obviously for the Find result format key it does not matter, but for the Replace result key there are different. You actually can’t get the matched values, which is an incredible shame.


My Favorite Formats


$l\t$c\t$f$e:\t$s\r\n is line, column, file with extension and first line of replacement


:clip_image009


$l\t$c\t$f$e:\t$0\t$s\r\n is line, column, file with extension, replaced value and first line of replacement


:clip_image011


$l\t$c\t($g)\t$f$e:\t$0\t$s\r\n is line, column, replacement length, file with extension, replacement value and first line of replacement:clip_image013


Enjoy creating custom formats for Replace in Files as well as Find in Files!