100

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

I don't mean regular expressions—which have their place—but plain old text find and replace. I know we can do it (at a pinch) with regular expressions using the \n tag, but we 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hawbsl
  • 15,313
  • 25
  • 73
  • 114
  • 3
    If you're at the point of considering extra software/writing a macro to do it I'd say just bite the bullet and learn how to use the regex find/replace. At least it's built in and has decent help... – Dave D Feb 16 '10 at 14:24
  • 3
    i know how to do regex but any text with a lot of punctuation in it is going to need an awful lot of regex fine tuning. i can't believe that's anyone's cup of tea for a simple find/replace operation – hawbsl Feb 16 '10 at 14:30
  • 1
    Vote [this MSConnect feature request](https://connect.microsoft.com/VisualStudio/feedback/details/661437/multiline-find-replace-functionality) to try to get a built-in solution for this. – Matt Faus Jul 18 '12 at 20:33
  • 1
    Any update on this question? From its comments it doesn't seem like the top answer completely works. It'd be great if there were a good solution for this... – JoeCool Jul 31 '12 at 16:56
  • Related meta question: *[Are questions specific to Excel VBA's IDE considered on-topic?](https://meta.stackoverflow.com/questions/420699/)*. In particular, [an answer](https://meta.stackoverflow.com/questions/420699/are-questions-specific-to-excel-vbas-ide-considered-on-topic/420701#420701) references this one. – Peter Mortensen Oct 07 '22 at 16:09
  • Macro support was [allegedly dropped starting with Visual Studio 2012](https://stackoverflow.com/questions/12062515/can-i-record-play-macros-in-visual-studio-2012-2013-2015-2017-2019). – Peter Mortensen Oct 07 '22 at 16:27

9 Answers9

67

I finally found it...

There isn't any need to download and load any external macro.

It’s 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 SampleUtilitiesFindLine. (It will open the Find box with your selection loaded in the "Find" field. Don't worry about truncated text shown in the "Find" field. Trust me, the field has it all... The Microsoft way of showing it may be... :) )
  4. Click on the "Quick Replace" button in the "Find And Replace" dialog box. Enter your replace with text.
  5. And click any of three buttons as per your requirement...and it’s done. :)

Hurray... it’s working. It may not be a straightforward way to do it, but you know with Microsoft. Nothing is straightforward and easy.. :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SV.
  • 730
  • 6
  • 3
  • This doesn't handle "find in files" right? Near as I can tell, it's single-file only? – Tom Lianza Feb 11 '11 at 01:41
  • 1
    @tlianza In VS2010, you can change the Quick Find dialog into Find in Files or Replace in Files using the buttons near the top of the screen. So, once the macro has populated the quick find dialog, you can switch to another view. – AaronSieb Apr 01 '11 at 19:58
  • 13
    I don't think this works if your replacement text also spans multiple lines. When you paste into the "Replace With" box, it gets truncated. – Andrew Corkery Aug 19 '11 at 09:52
  • Just have to link a keyboard shortcut to this macros and it will be simple like a common find and replace. Thank you very for this trick! – Samuel Aug 04 '12 at 03:22
  • @Kos I think you have to change Look in dropdown value to Current Document – Juozas Kontvainis Oct 29 '12 at 10:24
  • It doesn't work when your regex search must match across multiple lines. Voted down for that. – scrat.squirrel Jan 18 '13 at 14:03
  • Looks like multi-line find to me. – Carl Jan 21 '13 at 11:06
  • 9
    Macros have been retired in following Visual Studio versions so this no longer works. – David Burg Mar 10 '17 at 00:23
  • Macro support was [allegedly dropped starting with Visual Studio 2012](https://stackoverflow.com/questions/12062515/can-i-record-play-macros-in-visual-studio-2012-2013-2015-2017-2019). – Peter Mortensen Oct 07 '22 at 16:28
34

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

scrat.squirrel
  • 3,607
  • 26
  • 31
  • 2
    Well explained and works exactly how it says on the box. Given that macros are long since dead even at the time of posting, I believe this should be the accepted answer. – nathanchere Mar 31 '14 at 03:39
  • 1
    This should be the accepted answer – Be Chiller Too Oct 20 '21 at 15:53
  • 1
    Macro support was [allegedly dropped starting with Visual Studio 2012](https://stackoverflow.com/questions/12062515/can-i-record-play-macros-in-visual-studio-2012-2013-2015-2017-2019). – Peter Mortensen Oct 07 '22 at 16:26
18

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

James McCormack
  • 9,217
  • 3
  • 47
  • 57
Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79
  • 7
    How can we replace with a multi-line text? any ideas? – Tauseef Nov 05 '13 at 09:55
  • 2
    You may need to change `\n` to `\r\n` if your document has hidden carriage returns. – James McCormack Aug 14 '14 at 14:29
  • 1
    The question explicitly stated not using regular expressions. Regular expression has the draw back that not only you need to escape the line breaks you also need to escape all the match characters that would be regular expression escape characters. (vote down for not answer the question) – David Burg Mar 10 '17 at 00:20
13

I use this: Visual Studio Gallery Multiline Search and Replace

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
maurox
  • 1,254
  • 1
  • 19
  • 33
3

It’s provided by Microsoft only. Please check Multiline Search and Replace.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahesh Malpani
  • 1,782
  • 16
  • 27
  • 1
    This was my solution, still applicable to this date. – chakeda Apr 11 '18 at 21:37
  • The last paragraph *seems* contradictory. Can you [clarify](https://stackoverflow.com/posts/29276382/edit)? (But ********** ***without*** ********** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Oct 07 '22 at 16:19
1

Regarding the comment of Andrew Corkery:

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zaengi
  • 39
  • 5
  • But the question said: *"That doesn't involve knocking up our own macro."* – Peter Mortensen Oct 07 '22 at 16:14
  • Related: *[Why do I need 50 reputation to comment? What can I do instead?](https://meta.stackexchange.com/questions/214173/)*. – Peter Mortensen Oct 07 '22 at 16:14
  • Macro support was [allegedly dropped starting with Visual Studio 2012](https://stackoverflow.com/questions/12062515/can-i-record-play-macros-in-visual-studio-2012-2013-2015-2017-2019). – Peter Mortensen Oct 07 '22 at 16:26
1

The latest version (as of this posting) of Notepad++ does multi-line find/replace. With no macro support in Visual Studio any more, this is relevant now.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sliderhouserules
  • 3,415
  • 24
  • 32
1

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.

Fedor Alexander Steeman
  • 1,561
  • 3
  • 23
  • 47
0

this is deprecated and not supported any more according to this

iRef3at
  • 1
  • 1