Multiline Find & Replace in Visual Studio

Visual StudioIdeReplaceMultiline

Visual Studio Problem Overview


Can it be done? We're using VS2005 and VS2008 and VS2010.

I don't mean regular expressions - which have their place - but plain old text find & replace. I know we can do it (at a pinch) with regular expressions using the \n tag but prefer not to get tangled up in regex escape characters, plus there's a readability issue.

If it can't be done what plain and simple (free) alternative are people using? That doesn't involve knocking up our own macro.

Visual Studio Solutions


Solution 1 - Visual Studio

I finally found it..

No need to download and load any external macro.

Its working in Visual Studio 2008 with in-built macro at least. :)

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... :) )
  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. :)

Hurray.. its working.. May not be a straight forward way to do it but you know with MS.. nothing is straightforward and easy.. :)

Solution 2 - Visual Studio

This works today in Visual Studio 2012:

fooPatternToStart.*(.*\n)+?.*barPatternToEnd

See how the (.*\n)+? part does the match across multiple lines, non-greedy.
fooPatternToStart is some regex pattern on your start line, while barPatternToEnd is your pattern to find on another line below, possibly many lines below...

Example found here.

Simple and effective :)

Note: before VS2012, the pattern that worked was: fooPatternToStart.(.\n)+@.*barPatternToEnd

Solution 3 - Visual Studio

You might want to take a look at this blog entry:

Multiline Search and Replace in Visual Studio

Provides macro code to escape your search and show the VS find and replace dialog.

Solution 4 - Visual Studio

You can search for multiline expressions by clicking on the "Use Regular Expressions" checkbox in the "Find and Replace" dialog. Line breaks are then indicated by \n.

enter image description here

Solution 5 - Visual Studio

Solution 6 - Visual Studio

Its provided by microsoft only. Please check https://visualstudiogallery.msdn.microsoft.com/699CE302-B0D4-4083-BE0E-1682E873CEBF.

It uses regular expression only. But for those who don't know regex better to use it.

Solution 7 - Visual Studio

You could also open the files with UltraEdit which fully supports MultiLine replace. You can use the trial version if you only intend to use it once.

Solution 8 - Visual Studio

Regarding the comment of Andrew Corkery Aug 19 '11 at 9:52 above:

If you like to specify a multi-line replacement string as well, edit the macro code and set the replacement text as shown below. This will allow you to "fine-tune" your replacement with just the small modifications needed.

 Sub FindLine()
    Dim textSelection As TextSelection

    textSelection = DTE.ActiveDocument.Selection
    textSelection.CharLeft(True)
    DTE.ExecuteCommand("Edit.Find")
    DTE.Find.FindWhat = textSelection.Text

    'also preset replacement text with current selection
    DTE.Find.ReplaceWith = textSelection.Text
End Sub

Solution 9 - Visual Studio

The latest version (as of this posting) of Notepad++ does multi-line find/replace. I don't know if this was true back when this was asked. But with no macro support in Visual Studio anymore, this is relevant now.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhawbslView Question on Stackoverflow
Solution 1 - Visual StudioSV.View Answer on Stackoverflow
Solution 2 - Visual Studioscrat.squirrelView Answer on Stackoverflow
Solution 3 - Visual StudioRobert AdamsView Answer on Stackoverflow
Solution 4 - Visual StudioPhillip NganView Answer on Stackoverflow
Solution 5 - Visual StudiomauroxView Answer on Stackoverflow
Solution 6 - Visual StudioMahesh MalpaniView Answer on Stackoverflow
Solution 7 - Visual StudioFedor Alexander SteemanView Answer on Stackoverflow
Solution 8 - Visual StudiozaengiView Answer on Stackoverflow
Solution 9 - Visual StudiosliderhouserulesView Answer on Stackoverflow