Text Dedupe

Regex to Remove Empty Lines

Searching for a regex to strip empty or blank lines from a text file usually means you're already inside an editor and don't want to leave it. The good news: the pattern is short, it works almost everywhere that supports regex find-and-replace, and once you know what it actually matches, you can adapt it for the edge cases your text throws at you.

The general pattern

The core pattern for matching a completely empty line is:

^\s*$\n

Here's what each piece is doing:

  • ^ and $ anchor the match to the start and end of a line.
  • \s* matches zero or more whitespace characters - so it catches lines that are truly empty and lines that only contain spaces or tabs.
  • \n consumes the line's trailing newline character, so the empty line disappears entirely instead of leaving a blank gap where it used to be.

One caveat before you paste this anywhere: it assumes multiline mode is enabled, meaning^ and $ match the start/end of each line rather than only the start/end of the whole file. Most editors' Find & Replace dialogs default to this when regex mode is on, so it usually just works - but command-line tools like grep and sed handle line boundaries differently by design (they already operate one line at a time), so the multiline flag isn't something you need to think about there. The editor-specific sections below call out where this matters.

If you only want to match lines that are exactly empty (zero characters, not even whitespace) and leave whitespace-only lines alone, drop the \s* and use ^$\n instead. That distinction matters more than it looks: whitespace-only lines are technically not "empty," and some tools (including this one) treat those two cases as separate options for exactly this reason.

A second pattern worth knowing is for collapsing runs of blank lines down to a single one, rather than deleting them entirely:

\n\n+

replaced with \n\n. This matches two or more consecutive newlines and collapses them to exactly one blank line - useful when you want to tidy up excessive spacing between paragraphs without removing paragraph breaks altogether.

These two patterns work in any tool with real regex support - grep,sed, awk, and most code/text editors. The exact syntax for "replace with nothing" and whether \n is matchable at all varies by tool (some regex engines operate strictly per-line and won't let you match the newline character directly), which is why VS Code and Notepad++ below need slightly different approaches.

VS Code

  1. Open Find & Replace (Cmd/Ctrl+H).
  2. Click the .* icon to enable regex mode - plain Find & Replace treats your pattern as literal text otherwise.
  3. In the Find field, enter ^\s*$\n.
  4. Leave the Replace field empty.
  5. Click "Replace All."

VS Code's Find & Replace searches across the whole file but anchors ^ and$ to each line by default when regex mode is on, so the same^\s*$\n pattern from above works as-is - there's no separate multiline toggle to enable.

To collapse multiple blank lines into one instead of removing them, search for \n{3,} and replace with \n\n - this targets three or more consecutive newlines (i.e., two or more blank lines in a row) and reduces them to a single blank line.

Notepad++

Notepad++ has two paths, depending on what you've already got installed:

Built-in Find & Replace (no plugin required)

  1. Open Find & Replace (Ctrl+H).
  2. Set the search mode to "Regular expression" (radio button at the bottom of the dialog).
  3. Find: ^\s*$\r?\n - the \r? handles Windows-style CRLF line endings, which Notepad++ defaults to.
  4. Replace: leave empty.
  5. Click "Replace All."

TextFX plugin: if you already have TextFX installed, its "Delete Blank Lines" command under the TextFX menu does this in one click without touching the regex engine at all - worth using if you're not comfortable hand-writing the pattern, though it doesn't give you the fine control (whitespace-only vs. fully-empty, collapse-vs-delete) that the regex approach does.

Skip the regex entirely

If you'd rather not touch a regex pattern at all - or you want case-by-case control over whitespace-only lines, want to preview the result before committing, or you're working with a .csv/.json file rather than plain text - Remove Empty Lines does this with a paste-and-click interface, entirely in your browser, no upload required.

This guide is part of our full roundup offree tools for cleaning up messy text, covering duplicate lines, blank lines, extra spaces, and dirty CSV exports.

Frequently Asked Questions

Does ^\s*$\n work in every text editor?

The core idea is universal, but exact syntax varies. Some editors (like plain Notepad, or older versions of some IDEs) don't support regex find-and-replace at all, and others handle the newline character (\n) differently depending on whether they're in single-line or multiline matching mode. Always test on a small selection first if you're unsure how your specific editor's regex engine behaves.

What's the difference between ^\s*$\n and ^$\n?

^\s*$\n matches both fully empty lines and lines that contain only whitespace (spaces or tabs). ^$\n matches only lines with zero characters - a whitespace-only line won't match. Pick based on whether you want whitespace-only lines treated as "blank" or left alone.

How do I remove only consecutive blank lines, not all of them?

Use \n{3,} replaced with \n\n - this only touches runs of two or more blank lines in a row and collapses them to one, leaving single blank lines elsewhere in your text untouched.

Can I use this regex in grep or sed too?

Yes - sed -i '/^[[:space:]]*$/d' file.txt deletes all blank/whitespace-only lines from a file in place on Linux/macOS. grep -v '^[[:space:]]*$' file.txt does the same thing non-destructively, printing only the non-blank lines to your terminal.

Is a regex faster than using an online tool?

For a one-off edit inside a file you're already working in, regex is often faster since there's no context-switch to a browser. But if you need to process the same cleanup repeatedly, want a live preview before committing, or need to handle CSV/JSON specifically, a dedicated tool avoids re-deriving the pattern each time and reduces the risk of a typo silently corrupting your file.