To remove empty lines from multiple files at once, run a regex find and replace across the folder: find \r?\n[ \t]*(?=\r?\n) and replace it with nothing. That deletes every blank line, including lines that contain only spaces or tabs, and it works with both Windows (\r\n) and Unix (\n) line endings. If you only want to squeeze runs of blank lines down to a single one, find (\r?\n)[ \t]*(\r?\n)(?:[ \t]*\r?\n)+ and replace with $1$2. You can do either in a code editor, in PowerShell, or in a batch tool that shows a preview first.
Below are the patterns explained, step-by-step instructions for each method, and the files where you should not remove blank lines at all.
Pick the right pattern
| Goal | Find | Replace |
|---|---|---|
| Delete every blank or whitespace-only line | \r?\n[ \t]*(?=\r?\n) | (nothing) |
| Keep one blank line between blocks | (\r?\n)[ \t]*(\r?\n)(?:[ \t]*\r?\n)+ | $1$2 |
| Delete blank lines at the very start of a file | \A(?:[ \t]*\r?\n)+ | (nothing) |
| Delete extra blank lines at the end of a file | (\r?\n)(?:[ \t]*\r?\n)+\z | $1 |
The \A and \z anchors mean "very start" and "very end" of the file in .NET, PCRE and Boost. JavaScript-based tools do not support them; there, use ^ and $ with multiline mode switched off.
How the main pattern works
The pattern \r?\n[ \t]*(?=\r?\n) reads as: "a line break, then any spaces or tabs, as long as another line break comes straight after". In other words, it finds the line break that ends a line, plus the empty (or whitespace-only) line that follows, and removes them together. The lookahead (?=\r?\n) checks for the next line break without consuming it, so the line after the blank one keeps its own ending.
You will often see ^[ \t]*\r?\n with multiline mode recommended instead. It works in .NET and in editors that treat \r\n as one line break, but some engines, JavaScript among them, treat a lone \r as a line end too. On Windows files, ^ can then match between \r and \n, and the pattern strips the \n from every line ending, leaving bare \r characters behind. The lookahead version avoids that problem in every engine.
The [ \t]* part is what catches lines that look empty but contain stray spaces or tabs. If you want to keep those lines and remove only truly empty ones, drop it: \r?\n(?=\r?\n).
Method 1: Notepad++ (one file or a whole folder)
For a single open file, Notepad++ has a menu command: Edit → Line Operations → Remove Empty Lines (Containing Blank characters). For a folder:
- Press Ctrl+Shift+F to open Find in Files.
- Find what:
\r?\n[ \t]*(?=\r?\n). Leave Replace with empty. - Set Filters, for example
*.txt *.csv *.log, and the Directory. - Choose Regular expression as the search mode and tick In all sub-folders if needed.
- Click Find All to check the count, then Replace in Files.
Files that are not open are written straight to disk, with no undo, so take a copy first. More detail is in the Notepad++ Replace in Files guide.
Method 2: VS Code
- Open the folder and press Ctrl+Shift+H.
- Turn on regex with the
.*button (Alt+R). - Search:
\n[ \t]*(?=\n). VS Code normalizes line endings in its search, so\nmatches both Windows and Unix endings here. - Leave the replace box empty, set files to include, review the results and click Replace All.
Method 3: PowerShell
For a single file, the simplest approach keeps only lines with visible content:
(Get-Content .\notes.txt) | Where-Object { $_.Trim() -ne '' } | Set-Content .\notes.txt
The parentheses around Get-Content make PowerShell read the whole file before writing, which is what allows writing back to the same file.
For a folder of files:
Get-ChildItem -Recurse -File -Include *.txt,*.log | ForEach-Object { $lines = Get-Content $_.FullName; $kept = $lines | Where-Object { $_.Trim() -ne '' }; if ($kept.Count -ne $lines.Count) { Set-Content $_.FullName $kept } }
Two cautions. In Windows PowerShell 5.1, Set-Content writes in the ANSI code page by default, which can damage accented characters in UTF-8 files, so add -Encoding to match your files. And this rewrites every line ending as \r\n. The PowerShell guide covers encodings in more detail.
Method 4: a batch tool with preview and undo
In Bulk Replace, add a regex rule with \r?\n[ \t]*(?=\r?\n) as the find text and an empty replacement, choose the folder and file types, and check the before/after diff. The preview shows exactly which lines disappear in each file, so you can untick a file where the blank lines matter. Each file keeps its original encoding and line endings, and the run is backed up first, so a mistake is one click to undo.
Bulk Replace can stack a second rule in the same run, for example one that also trims trailing spaces. See removing trailing whitespace from all files for that pattern.
Where blank lines matter
Blank lines are not always decoration. Check these file types before you run a folder-wide clean-up:
| File type | Why blank lines matter | Safe approach |
|---|---|---|
| Markdown (.md) | A blank line separates paragraphs, lists and code blocks | Collapse runs to one blank line; never remove all |
| SRT subtitles (.srt) | A blank line ends each subtitle cue | Exclude, or only collapse |
| Email and HTTP headers (.eml) | A blank line separates headers from the body | Exclude |
| Python (.py) | Harmless to code, but style guides expect blank lines between functions | Collapse rather than remove |
| INI and config files | Usually safe; blank lines only separate sections visually | Remove or collapse |
| Logs and data exports (.log, .txt, .csv) | Usually safe, and blank rows can break imports | Remove |
Content inside multi-line strings, such as SQL in a code file or a <pre> block in HTML, also keeps its blank lines on purpose. A preview is the easiest way to spot those cases.
Check the result
- Search the folder again with the same pattern. The count should be zero.
- Open a few files of each type and check paragraphs, cues or sections still look right.
- If files are under version control, review
git diff --statto confirm only the expected files changed. - For data files, re-run the import or script that uses them.
If the file count is lower than you expected, the files may be filtered out or saved in an encoding your tool did not read correctly. If it is higher, the pattern probably matched blank lines inside files where they were meaningful, so check the table above and narrow the file types.
Quick reference
- Remove all blank lines:
\r?\n[ \t]*(?=\r?\n)→ nothing - Remove only truly empty lines:
\r?\n(?=\r?\n)→ nothing - Collapse to one blank line:
(\r?\n)[ \t]*(\r?\n)(?:[ \t]*\r?\n)+→$1$2 - PowerShell, one file:
(Get-Content f.txt) | Where-Object { $_.Trim() -ne '' } | Set-Content f.txt
Frequently asked questions
What regex removes blank lines?
Find \r?\n[ \t]*(?=\r?\n) and replace with nothing. It removes empty lines and lines that contain only spaces or tabs, and works with both Windows and Unix line endings.
How do I collapse several blank lines into one?
Find (\r?\n)[ \t]*(\r?\n)(?:[ \t]*\r?\n)+ and replace with $1$2. Runs of two or more blank lines become a single blank line, and the original line endings are kept.
Can PowerShell remove empty lines?
Yes. (Get-Content file.txt) | Where-Object { $_.Trim() -ne '' } | Set-Content file.txt keeps only lines that contain something.
