Often it is useful to do a global search and replace in a directory of files. For a search string on a single line this is trivial. Just hit Ctrl-Shift-H or “Edit -> Find and Replace -> Replace in Files”. Then enter the necessary information and do the replacement.
It becomes more tricky when you have a multiline search or replacement string. With the standard options in the search and replace box you can’t do this. But select Regular Expressions in the Find Options section and a multi-line string becomes possible.
Now you can use Regular Expressions in the search. Enter your search and replace string as normal but enter ‘\n’ where you want a newline to appear. Visual Studio can now find and replace multi-line strings!
#1 by Kenny on April 15, 2008 - 6:39 pm
Thanks for the tip. It led me down the right path. Your solution is not quite complete, though.
The Text you enter for “Find What” has to be a valid regular expression. You will have to escape lots of special characters, like parenthesis and braces when searching for code blocks.
#2 by Waqqas Farooq on June 4, 2008 - 7:24 am
Is it possible we could use regular expressions only for the replace text and not the find text? e.g. I have the text “\n\n\n” I want to search for \n and replace it to line feeds.
#3 by Ross Youngblood on October 20, 2008 - 12:58 pm
This doesn’t appear to work in Visual Studio 6.0 (What we are stuck with).
(Regexp searches don’t work with \n.
or \r or \r\n… that… that is I haven’t gotten them to work yet.
#4 by Simon Morris on June 1, 2009 - 12:54 pm
This was a useful post, thanks. I used this technique to reformat some xml files from:
to:
Find: OldElement{.*\n.*\n.*\n.*\n.*}OldElement
Replace: NewElement\1NewElement
#5 by Jared on June 10, 2009 - 8:37 pm
This is ridiculous. What was MS thinking?
#6 by Eric on June 19, 2009 - 6:53 pm
I agree with Jared. I mean, this is the kind of standard feature in programs like Dreamweaver since…I don’t know…1998.
I mean, really, you can’t search and replace multiple lines of text any easier than this?
Here’s a better way: Open up your site in Dreamweaver, use its find-and-replace..then close Dreamweaver and re-open VS. Tada! No regular expressions writing.
#7 by Thomas K. Nielsen on September 8, 2009 - 7:29 am
Just what I needed. Thanks for this tip.
#8 by Danzig on March 8, 2010 - 2:16 pm
Fully agree with Jared and Eric. This is pathetic.