Bulk Replace
How-to

How to Find and Replace a Block of Multiple Lines in Many Files

Replace a block that spans several lines, such as a menu, script tag or comment, across many files. Line-ending pitfalls, regex patterns and safe steps.

Updated 5 minute read The Bulk Replace team

To replace multiple lines of text in multiple files, you need a search that can match across line breaks. Either paste the exact block into a tool that supports multi-line search, or write a regular expression that matches the block from a fixed start to a fixed end, such as <nav[\s\S]*?</nav>. The usual reason a multi-line search finds nothing is that the files use different line endings or indentation than the text you typed, so build in some flexibility with \r?\n and \s*.

This guide explains how line breaks really work in files, gives patterns for common block replacements, and shows how to apply them safely across a whole folder.

Why multi-line replace is harder than it looks

On screen, a block of text looks the same in every file. Underneath, small invisible differences break exact matches:

  • Line endings. Windows files normally end each line with two characters, carriage return and line feed (\r\n, or CRLF). Files from Linux, macOS or Git with certain settings use just \n (LF). A pattern that expects one will not match the other.
  • Indentation. One file indents with four spaces, another with a tab, another with two spaces.
  • Trailing spaces. An invisible space at the end of a line breaks an exact match.
  • Small content differences. A menu that has an "active" class on a different item in each page is not the same block of text in every file.

The solution is to match the block by its structure, not its exact characters.

The key regex building blocks

PatternMatchesUse it for
\r?\nA line break, Windows or Unix styleJoining lines in a pattern
\s*Any amount of whitespace, including line breaksFlexible indentation and gaps
[\s\S]*?Any characters, including line breaks, as few as possible"Everything between A and B"
(?s).*?Same as above, using single-line modeEngines that support inline flags
[^\r\n]*The rest of one lineMatching within a single line

The ? after * makes the match lazy, so it stops at the first possible end point rather than the last one in the file. Without it, a pattern like <nav>[\s\S]*</nav> on a page with two nav blocks would swallow everything from the first opening tag to the last closing tag.

Common block replacements

Replace a whole HTML element

Find: <footer\b[\s\S]*?</footer>

Replace with the new footer markup. The \b stops it matching tags like <footer-note>. This works as long as the element does not contain another element with the same name, which is true for most headers, footers and nav bars.

Replace a block between marker comments

If your pages have comments around a section, use them as anchors:

Find: <!-- MENU START -->[\s\S]*?<!-- MENU END -->

Replace with the markers plus the new content, so the next update is just as easy. Our guide to replacing the header and footer in multiple HTML files works through this approach for static sites.

Remove a script tag that spans lines

Find: <script\b[^>]*>\s*\(function\(\)\s*\{[\s\S]*?oldtracker[\s\S]*?</script>\s*

Replace with nothing. Anchoring on something unique inside the block (here oldtracker) makes sure you only remove the script you mean. Check the preview carefully: the lazy [\s\S]*? before oldtracker can still reach across an earlier script, so the more specific the start of the pattern, the safer.

Delete a multi-line comment block

Find: /\*[\s\S]*?\*/

This matches C-style comments in CSS, JavaScript, PHP, C# and Java. Be careful: the same characters can appear inside strings, such as a URL containing /*, so review the matches before replacing.

Join a line with the next one

Find: ,\r?\n\s*, replace with ,

Lines ending in a comma are joined with the following line, and the next line's indentation is removed.

Keeping line endings consistent in the replacement

When your replacement text contains several lines, the new lines get whatever line break your tool inserts. If you type a block into a Windows tool, that is usually CRLF. In a file that uses LF, you end up with mixed line endings. Two ways to avoid that:

  • Check the line endings of your files first. In VS Code and Notepad++, the status bar shows CRLF or LF for the current file.
  • Use escape sequences in a regex replacement where your tool supports them, writing \n for LF files and \r\n for CRLF files.

Tool by tool

  • PowerShell: read each file with Get-Content -Raw so it is one string, then use -replace with a pattern such as '(?s)<nav\b.*?</nav>'. Without -Raw, you get separate lines and multi-line patterns cannot match. See our PowerShell replace guide for the full loop.
  • VS Code: turn on regex in the Search view and include \n in the pattern. VS Code treats \n as matching Windows line breaks too. Press Shift+Enter to type an actual new line in the search box.
  • Notepad++: in Find in Files, choose Regular expression mode, and tick . matches newline if your pattern uses . to span lines. \R matches any line ending.

A safer way to rewrite whole blocks

Multi-line patterns are the ones most likely to match more than you intended, so seeing the result before writing matters. Bulk Replace supports multi-line replacement for HTML blocks, comments and repeated headers and footers, and lets you rewrite or remove whole sections across a folder tree. The before/after diff shows exactly which lines each match covers in every file, and you can untick any file where the block was different. Bulk Replace backs up every file before it changes, so a bad pattern can be undone in one click.

When the blocks are not identical

If the block differs slightly from file to file, do not try to write one pattern that covers every variation. Group the files by version, run a separate rule for each group, and check each group's match count. Two simple rules are easier to trust than one complicated one.

Checklist before you run it

  1. Open two or three files and confirm the block's start and end markers are identical in each.
  2. Check the line ending type (CRLF or LF) shown in your editor's status bar.
  3. Use a lazy *?, not a greedy *.
  4. Run the search alone. The match count should equal the number of files (or blocks) you expect.
  5. Look at the longest match. If it covers more than the block, tighten the start or end of the pattern.
  6. Back up, replace, then open a few pages to check the layout.

For more on what to do when the search still says zero matches, see find and replace not finding text.

Frequently asked questions

Why does my multi-line search find nothing?

Usually the line endings or indentation differ. Windows files use \r\n, many others use \n, and tabs versus spaces also break an exact match. Use \r?\n and \s* in a regex to allow for both.

How do I match any text across lines in regex?

Use [\s\S]*? or turn on single-line mode with (?s) so the dot also matches line breaks. The ? keeps the match as short as possible.

Can Notepad++ replace multiple lines in files?

Yes. In Regular expression mode you can type \r\n in the Find what box, and Find in Files applies it across a folder.

Get three guides like this every month, plus software update notices.Subscribe →