Notepad++

How to Remove Empty Lines in Notepad++

How to Remove Empty Lines in Notepad++

The quickest route is the Edit menu. Go to Edit > Line Operations > Remove Empty Lines and the file is cleaned in one click. A regular expression such as ^\h*\R takes over for the cases the menu can’t handle, like selections and whole folders.

The menu has two commands for this, and they don’t behave the same. Remove Empty Lines deletes lines with no characters at all. Remove Empty Lines (Containing Blank characters) goes further and also deletes lines holding only spaces or tabs, according to the Notepad++ User Manual.

Log files and CSV exports collect both kinds of blank line, and pasted text does too.

Both commands act on the whole document, so a selection does not limit them.

How do you remove empty lines in Notepad++ with the built-in commands?

YouTube player

Open Edit > Line Operations and choose Remove Empty Lines. Notepad++ deletes every line that contains no characters, from the top of the file to the bottom.

  1. Open the file in Notepad++.
  2. Click Edit, then Line Operations.
  3. Pick Remove Empty Lines for lines with zero characters, or Remove Empty Lines (Containing Blank characters) for lines that hold only spaces or tabs.
  4. Scroll through the result. If gaps remain, turn on the whitespace and end-of-line marks under View > Show Symbol to see what is left inside them.

Empty lines versus lines with blank characters

YouTube player

Remove Empty Lines deletes lines that have no characters before the line ending. The Containing Blank characters version does the same and also deletes lines made only of spaces or tabs.

A line holding a single space looks empty on screen, but the first command skips it. That’s why a gap sometimes survives the menu command.

What the menu commands leave alone

YouTube player

Both commands work on the whole document. A selection does not narrow them.

The Notepad++ User Manual describes them as removing lines from the entire document, while Remove Duplicate Lines in the same submenu does respect a selection. A Notepad++ Community user in February 2019 selected one block, ran the command, and lost every empty line in the file.

The manual describes the blank-characters command in terms of spaces and tabs only. Its horizontal-space class (\h) also covers the non-breaking space (\xA0), so if a gap survives the command, check for that character. The \h pattern in the next section removes it.

Which regular expression removes empty lines in Notepad++?

YouTube player

Run the patterns below in Regular expression mode with Replace with left empty. ^\R removes lines that contain no characters, and ^\h*\R also catches lines that contain only spaces or tabs.

Press Ctrl+H to open the Replace tab, paste the pattern into Find what, set Search Mode to Regular expression, and tick Wrap around. Then click Replace All.

Wrap around matters. With it ticked, Replace All makes one pass over the whole file, and with it unticked it starts at the caret and stops at the end of the file (Notepad++ User Manual).

PatternRemovesLine endings handled
^\RLines with no charactersCR LF, LF, CR
^\h*\REmpty lines, plus lines with spaces, tabs or non-breaking spacesCR LF, LF, CR
^\s*\r\nEmpty and whitespace-only linesCR LF only
^\r\nLines with no charactersCR LF only

The ^\s*\r\n pattern comes from a Notepad++ Community thread on version 8.8.3, and ^\r\n from a Computer Hope guide. Both hard-code CR LF, so they match nothing in a file that ends lines with LF alone.

Pattern for completely empty lines

The ^ anchors the match to the start of a line, and \R matches any newline sequence. Put together, they match a line whose first character is already the line ending.

New to Notepad++ or just need a quick reference? Essential shortcuts, regex for Find & Replace, and advanced editing - including Ctrl+H, Column Mode, and Macro Recorder - is on one page in the Notepad++ Cheat Sheet.

A 2019 Community thread used ^$\R for this job. Alan Kilborn, a long-time forum regular, pointed out in the same thread that the shorter ^\R gives the same result.

Pattern for lines with spaces or tabs

YouTube player

Per the User Manual, the \h class covers the tab, the space and the non-breaking space.

\s looks similar but is wider. It also matches line separators, so ^\s*\r\n can reach across several lines at once, while ^\h*\R stays on one line.

Which regex engine Notepad++ uses

Notepad++ runs its search on the Boost regex library, not on PCRE itself. The User Manual lists Boost 1.90 as of version 8.9.1. The Notepad++ Community FAQ gives 1.80 as of 8.4.7 and 1.85 as of 8.7.9, while regular-expressions.info dates Boost 1.85 or later to 8.6.6, so the sources differ by a few releases.

Test a pattern inside Notepad++ rather than in an online regex tester. The manual warns that testers claiming PCRE support can behave differently.

Older Stack Overflow answers from 2011 say Notepad++ regex works one line at a time and cannot match line breaks. The current manual says the opposite, and its own examples match line endings with \R.

The Community FAQ dates the present engine to version 6.0, improved through 6.4.2. Answers written for earlier builds describe a different engine, so run any pattern from an old answer on a copy of the file first.

Why do line endings change the pattern?

A pattern written for CR LF matches nothing in a file that ends its lines with LF alone. The line ending type decides whether \r\n, \n or \r belongs in the pattern.

Windows files use CR LF, which the status bar shows as Windows (CR LF) and a pattern writes as \r\n. Unix and Linux files use LF, shown as Unix (LF) and written \n. Classic Macintosh files use a lone CR, shown as Macintosh (CR) and written \r, though macOS itself uses LF, so CR-only files are rare.

The \R escape matches any of these, including a mixed file. It cannot go inside a square-bracket character class, though. The manual warns that [\t\R] reads the R as a literal letter.

Regular expression mode or Extended mode

Regular expression mode supports anchors such as ^ and $, so a pattern can target lines that are empty and leave every other line ending alone.

Extended mode turns escapes like \n, \r and \t into the actual characters, but it has no anchors. Replacing \r\n with nothing deletes every line ending in the file.

That is what happened to a user in a Notepad++ Community thread, where an Extended-mode replace of \r\n left the text running together on one line. The reply was that Extended mode finds every line ending, empty line or not.

How to see and fix the line ending

YouTube player

The status bar shows the active type. Turn on the end-of-line marks under View > Show Symbol to see CR LF, LF or CR at the end of each line.

Edit > EOL Conversion converts the whole file to one type. When the type you want is greyed out, the manual says to pick a different one first and then switch back, which also cleans up a file with mixed endings.

How do you remove empty lines only in a selection?

The menu commands can’t do this, so you have to go through the Replace dialog. A bookmark pass and a plugin also work, but the dialog is the simplest.

Regex with In selection

  1. Select the lines you want to clean.
  2. Press Ctrl+H and tick In selection.
  3. Enter ^\h*\R in Find what, leave Replace with empty, and set Search Mode to Regular expression.
  4. Click Replace All.

In selection limits Count, Replace All and Mark All to the selected text. Find Next keeps searching the whole document.

Large selections come with a catch. When more than 1024 characters are selected as the dialog opens, Notepad++ ticks In selection by itself and copies the selected text into Find what. Replace All then reports 1 occurrence until you overwrite Find what with your pattern (Notepad++ User Manual).

Bookmark route

YouTube player

Open the Mark tab (Ctrl+M) and enter ^$ for truly empty lines, or ^\h*$ for lines that only look empty. Tick Bookmark line, set Regular expression mode, tick In selection if needed, and click Mark All.

Check the bookmark margin, then run Search > Bookmark > Remove Bookmarked Lines. End the pattern with $ rather than \R here, because a hit that spans lines bookmarks every line in the span.

TextFX plugin

TextFX adds TextFX Edit > Delete Blank Lines, which works on a selection. Older Stack Overflow answers say it ships with Notepad++.

A Notepad++ Community moderator wrote in February 2019 that it is missing from the Plugins Admin list in 64-bit builds. The same thread warns that a 32-bit install kept for TextFX can turn into a 64-bit install after an update, and the plugin disappears with it.

On a 64-bit install I’d skip the plugin. The regex with In selection does the same job without a dependency.

How do you remove empty lines from all open files or a whole folder?

YouTube player

Use Replace All in All Opened Documents for files already open in tabs, and Find in Files for a folder on disk. Both accept the same regular expression as a single file.

Forum regular guy038 suggests ^(\h*\R)+ with an empty Replace with field for multi-file runs. One match takes a whole run of blank lines, and \R keeps it working when some files use CR LF and others LF.

Replace All in All Opened Documents

On the Replace tab, set Regular expression mode and click Replace All in All Opened Documents, not Replace All. The two buttons sit side by side, and a Notepad++ Community thread from 2018 already flagged how easy it is to hit the wrong one.

The confirmation prompt has moved around over the years. Users reported an unavoidable prompt from around version 7.8.8 to 7.8.9 in 2020, a Preferences option existed through 8.9.8.1, and the User Manual lists a checkbox beside the button as new in 8.9.9.

Find in Files for a folder

Point Directory at the top folder. Tick In all sub-folders to reach nested ones, and In hidden folders if the files you want sit in those.

Filters takes a space-separated wildcard list such as *.log *.csv, and a leading ! excludes a pattern, as in !*.bin *.*. Leave Replace with empty and click Replace in Files, which asks you to confirm first.

If a file has unsaved edits in a tab, Find in Files searches what Notepad++ holds in that tab, not what is on disk (User Manual). Copy the folder before you run it.

Files where blank lines mean something

Some formats use empty lines on purpose, so check before you strip them. A log may separate its records with one blank line, and a CSV export may carry an empty row that downstream code expects.

Run Count on the Find tab, or Find All in All Opened Documents, to see how many lines a pattern would hit. Compare that number with what you expect before you replace.

How do you reduce several blank lines to one in Notepad++?

YouTube player

Search for (\R){3,} and replace it with \1\1, in Regular expression mode. Every run of two or more blank lines shrinks to one, and a lone blank line stays put.

Put (\R){3,} in Find what and \1\1 in Replace with.

Scott Sumner posted this pair in a Notepad++ Community thread. Three line breaks in a row means the end of a text line plus two blank lines, and writing back two leaves the text line and one blank line.

The group captures a line break the file already uses, so the replacement does not hard-code CR LF or LF. The built-in commands cannot do this job at all, because they remove every blank line (the thread’s original poster wanted one kept).

Variants for other files

Alan Kilborn answered a similar question with ^(\R)+ replaced by ${1}, which does the same for runs of bare line breaks.

For blank lines that hold spaces or tabs, guy038 posted (\h*\R){3,}|\h+\R replaced by \r\n?1\r\n for Windows files, or \n?1\n for Unix files. It also trims trailing blanks on text lines.

The ?1 part is a conditional replacement from the User Manual’s substitution syntax. Unlike the first pair, this one types the line ending out, so match it to the file.

One pass, not several

Replace All makes one pass over the document when Wrap around is ticked (User Manual).

That is why the Extended-mode trick of swapping \r\n\r\n for \r\n needs repeating. A Stack Overflow commenter who swapped \r\r for \r reported running it several times before every pair was gone. Each pass collapses only pairs of line breaks, so a long run needs another round.

When does removing empty lines go wrong?

Usually the Search Mode setting or the line ending is to blame. Now and then the pattern itself matches more than a blank line.

Why a pattern matches nothing

  • Search Mode is still on Normal or Extended, so ^ and \R are read as literal text.
  • The pattern assumes the wrong line ending, as when a user in a Notepad++ Community thread searched \n\n and found nothing because the Windows file held \r\n\r\n.
  • The expression is malformed. The red “Invalid regular expression” message appears only when you click Find Next, so test with that button first (User Manual).
  • Hidden characters came along when the pattern was pasted. Since version 8.9.1 the status bar warns about invisible characters in pasted Find what text.

Why a pattern deletes too much

Short answers online often use ^\s* on its own, but the manual defines \s as spaces, tabs and line separators. With an empty Replace with field it strips the indentation from every line, not only blank ones.

An optional \R causes a different problem. The manual’s own fix for a missing last line is \R?, which suits patterns that delete text lines. In an empty-line pattern it makes the match zero-length, so ^\h*\R? also removes leading spaces from lines that have text.

What happens at the last line

A blank last line with no line break survives ^\R+ and ^(\h*\R)+. A forum thread on multi-file cleanup explains why: the character that needs deleting is the newline before that line, not anything inside it.

The generic fix from that thread is ^\h*\R|\R\h*\z, where \z is the end-of-file anchor in the User Manual. Forum regular PeterJones, who posted it, noted that an empty final line otherwise stays put however many times you click Replace All.

Undo and recovery

After Replace All in All Opened Documents, Ctrl+Z reverts only the active tab, so you repeat it tab by tab (Notepad++ Community, 2020).

Replace in Files is harsher. A Community thread notes there is no undo for files that are not open in Notepad++, though files open in tabs can be undone one tab at a time.

For a single file, sources are not unanimous on how many presses a Replace All takes to undo. Claudia Frank warned it may take as many presses as there were replacements, while other users treat Ctrl+Z then Ctrl+Y as a quick way to jump to the last change.

Treat undo as a convenience, not a backup. Save a copy before any large replace.

Method comparison

MethodScopeBest forMain risk
Built-in commandsWhole documentQuick cleanup of one fileCannot limit to a selection
Regex Replace AllFile, selection or all open tabsControl over what counts as blankWrong Search Mode or an over-wide pattern
Bookmark routeCurrent document or selectionReviewing lines before deletingMulti-line hits bookmark extra lines
Replace in FilesFolder tree, set by FiltersMany files on diskNo undo for files not open in Notepad++
TextFX pluginSelection32-bit installs onlyMissing from the 64-bit Plugins Admin list (2019 thread)

For one whole file, the menu command wins on speed. Reach for a pattern once a selection, a blank line you want to keep, or a folder enters the picture.

FAQ About Removing Empty Lines in Notepad++

How do I trim spaces at the ends of lines at the same time?

Run Edit > Blank Operations > Trim Trailing Space to strip spaces and tabs from the end of every line. Trim Leading and Trailing Spaces covers both ends.

With one stream selection active, the command applies only to the lines of that selection, and a column-mode selection does nothing (Notepad++ User Manual).

Can a pattern remove blank lines only at the top of a file?

Yes. Search for \A(\h*\R)+ in Regular expression mode and leave Replace with empty.

The \A anchor ties the match to the start of the file, so only the leading run of blank lines goes.

How can I count blank lines before deleting them?

On the Find tab, set Regular expression mode, enter ^\h*\R and click Count. Each match is one blank line.

Find All in Current Document lists the same lines by number in the Search results window.

Why did Replace All change text outside my selection?

With In selection unticked and Wrap around off, Replace All runs from the start of the selection to the end of the file. It does not stop where the selection ends.

Tick In selection to confine the replacement to the selected lines (Notepad++ User Manual).

Turning Empty Line Removal Into a Routine

For a weekly cleanup, a keyboard shortcut on Remove Empty Lines (Containing Blank characters) does the job. The Shortcut Mapper takes one assignment and stores no search settings that can go stale.

Put a recorded macro on selections and collapse-to-one patterns, and leave folder runs as a manual step. A macro freezes its settings, since the User Manual says the recorder snapshots Find what, Replace with and the checkbox states whenever a Find family action runs.

So In selection and a typed \r\n replay exactly as recorded. Write recorded patterns with \R, and one macro serves CR LF and LF files alike.

This position was checked against the User Manual covering version 8.9.9, in November 2026.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.