Helixoft Blog

Peter Macej - lead developer of VSdocman - talks about Visual Studio tips and automation

Multiline Search and Replace in Visual Studio (2013-2005)

It's been 8 years since I wrote a Multiline Search and Replace macro for Visual Studio. At that time, I only needed some multi-line replacements and I had no idea how popular the macro could be. A lot of comments and visits proved that this is really something MS should implement directly in VS. Now we have VS 2013 and multiline find and replace still isn't there. What's worse, support for macros was removed from VS 2012 and higher, but that's another topic. So neither my macro helps now.

So I converted the macro to Visual Studio package (extension) and now you can use it in Visual Studio 2005-2013. You can download it here.

This extension allows multiline search and replace in Visual Studio. It is a user-friendly GUI wrapper over standard VS "Find" and "Find in Files" dialog. You don't have to use the regex syntax, the extension takes care of it.

After installing, a new Multiline Find/Replace menu item is created under Edit - Find and Replace.

When you invoke the command, you can insert multiline text in both Find what and Replace with fields.

According to which find or replace button you press, the standard VS find/replace dialog appears. All required fields are correctly prepopulated. That means Find what and Replace with fields contain the regular expressions for newlines. Moreover, the Use Regular Expressions option is automatically selected.

Now you know it. You can search and replace as usual.

Predefined keyboard shortcut for the multiline search command is Ctrl +  Alt + F. If you want to change it (for example because the shortcut is already used) you can do it in Tools - Options... menu. Navigate to Environment - Keyboard. Type "multiline" in Show commands containing field. You should see Edit.MultilineFindReplace command for which you can change the shortcut.

To add the command to your toolbar, right click on the toolbar and select Customize. Switch to Commands tab. Select Toolbar: Standard and press Add command. Select Edit category and Multiline Find/Replace command.

And now it's always at your hand.

Comments   

# Robert Hamilton 2014-03-12 15:32
I am using Visual Studio 2012 (Update 3, if it matters).

First, thank you for making converting this so that the macro can be used in newer versions of visual studio. All of that seems to work. The macro comes up with I choose it from the edit menu and when I press the Find >> Find in Files >> Replace >> and Replace in Files >> buttons it fills out the find/replace dialogs for me.

What does not work:

1.) Control + Alt + F brings up the interactive F# window. Maybe there is someway to override that in your MSI.

2.) When I bring up the dialog a 2nd time it does not take the text highlighted. Instead, "Find What" contains the text from the first time I ran it.

These two are no big deal I can copy paste and I have already assigned Control + Alt + F to your dialog manually. 3.) Is a show stopper, though.

3.) It does not work. Once I tell the Microsoft replace to use the regex generated by the tool, it finds nothing. After some digging, I found if I take your string and paste it into notepad and then (ironically) use the replace dialog to change the \n to \r\n and then paste that back into visual studio, it works. I assume this is because most windows text documents are delimited with CR+LF pairs. CR i 0x0D or '\r' and LF is 0x0A or '\n'. I know there is a way in regex to match just \n or just \r or \r\n or \n\r. If you can author that bit of regex and use that to match the lines, it would be work under all scenarios. (check out the "NewLine" topic on Wikipedia for more reference)
Reply | Reply with quote | Quote
# Peter Macej 2014-03-14 21:13
Quoting Robert Hamilton:

1.) Control + Alt + F brings up the interactive F# window. Maybe there is someway to override that in your MSI.


This is intentional. If the shortcut is already used, it would be considered quite aggressive from the extension to steal it. The Control + Alt + F shortcut is also used by other extensions, so it's up to the user to resolve it.

Quoting Robert Hamilton:

2.) When I bring up the dialog a 2nd time it does not take the text highlighted. Instead, "Find What" contains the text from the first time I ran it.


I have added this feature in version 1.2.

Quoting Robert Hamilton:

3.) It does not work. Once I tell the Microsoft replace to use the regex generated by the tool, it finds nothing. After some digging, I found if I take your string and paste it into notepad and then (ironically) use the replace dialog to change the \n to \r\n and then paste that back into visual studio, it works. I assume this is because most windows text documents are delimited with CR+LF pairs. CR i 0x0D or '\r' and LF is 0x0A or '\n'. I know there is a way in regex to match just \n or just \r or \r\n or \n\r. If you can author that bit of regex and use that to match the lines, it would be work under all scenarios. (check out the "NewLine" topic on Wikipedia for more reference)


I have fixed it in version 1.2. The problem was that regex behaves differently in VS 2010 and higher then in VS 2005/2008. In VS 2005/2008 you only need \n and must not use \r. Starting with VS 2010 you need \r\n or any combination.

The new version 1.2 is available at http://visualstudiogallery.msdn.microsoft.com/699CE302-B0D4-4083-BE0E-1682E873CEBF
Reply | Reply with quote | Quote
# Peter Macej 2014-03-29 14:29
Quoting Peter Macej:
The problem was that regex behaves differently in VS 2010 and higher then in VS 2005/2008.


Just a small correction. Regex in Find dialogs behaves differently in VS 2012 and higher, not in VS 2010 and higher.
Reply | Reply with quote | Quote
# Bill Woodruff 2014-03-29 14:25
Many thanks for this great Extension !
Reply | Reply with quote | Quote
# Moshe 2014-08-01 18:26
Hi Peter, I've installed this extension but it only shows in my VS2010 but not VS2013 (Express). Any ideas on that?

Thank you!
Moshe
Reply | Reply with quote | Quote
# Peter Macej 2014-08-03 22:11
Extensions don't work with Express editions of Visual Studio.
Reply | Reply with quote | Quote
# Robert Adams 2014-11-24 18:55
Hi Peter,

Thanks for the great work - I've used the macro version of your code in previous versions of VS.

Are you hosting this project on github / bitbucket / codeplex / etc? If so could you share the URI?

Thanks,

Rob.
Reply | Reply with quote | Quote
# Peter Macej 2014-11-24 20:14
No, the source code is not available.
Reply | Reply with quote | Quote
# Stefan Manov 2014-12-02 14:10
Thank you for the extension!
I'd like to report a minor bug - when using it in Windows 7 with 125% text size (which is my default, since I like high PPI displays but hate miniature letters), half the buttons' text partially disappears, i.e. 'Replac' and 'Replace in' is what I see, and I can see no way to extend the buttons to make it visible.
Reply | Reply with quote | Quote
# Peter Macej 2014-12-19 10:15
Fixed in version 1.6.
Reply | Reply with quote | Quote
# Stefan Manov 2014-12-19 14:03
Thanks!
Reply | Reply with quote | Quote

Add comment


Security code
Refresh

 

Start generating your .NET documentation now!
DOWNLOAD
Free, fully functional trial