Multi-line Search and Replace in Visual Studio

Written by Patrick on January 14, 2008 – 8:54 pm -

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!

Multi-line search and replace


Tags: , ,
Posted in Visual Studio |

2 Comments to “Multi-line Search and Replace in Visual Studio”

  1. Kenny Says:

    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. Waqqas Farooq Says:

    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.

Leave a Comment