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.
#9 by Ankit on July 7, 2010 - 3:21 pm
Eric, you are a genius. and I am a fool. Why did I not think of it?
#10 by Ankit on July 7, 2010 - 3:22 pm
Eric, you are a genius. and I am a fool. Why did I not think of it?
#11 by Ben on December 10, 2010 - 2:54 am
Visual Studio Search & Replace = #Fail
Does anyone know of another easy way of search and replacing blocks of code in a project without Dreamweaver?
Many thanks
#12 by Matt on December 21, 2010 - 3:17 am
No RegEx required –
) 4) Click on “Quick Replace” button on “Find And Replace” dialog box. Enter your replace with text. 5) And Click any of 3 buttons as per your requirement…and its done.
Steps : 1) Select text you want to find. 2) Press “Alt+F8″ or open “Tools -> Macros -> Macro Explorer” 3) Double click “Sample -> Utilities -> FindLine”. (It will open Find box with your selection loaded in “Find” field. Don’t worry about truncated text shown in “Find” field. Trust me, field has it all..MS way of showing it may be…
http://stackoverflow.com/questions/2273434/multiline-find-replace-in-visual-studio
Works for me
#13 by Fedor Steeman on January 20, 2012 - 12:44 am
You could also open the files with UltraEdit which fully supports MultiLine replace.