Skip to content

How to Remove Empty Lines and Blank Lines From Text

What Are Empty Lines?

An empty line is a line that contains no visible characters -- just a line break character (or whitespace characters followed by a line break). In source code, documents, and data files, empty lines serve as visual separators between sections. But when they accumulate excessively -- 3, 5, or 10 blank lines between paragraphs -- they create bloated, hard-to-navigate content.

Strip empty lines from any text with WritePadPro's Remove Empty Lines tool -- paste your text and collapse excessive blank lines down to clean, single-spaced content.

Empty Lines vs Whitespace-Only Lines

There is an important distinction between truly empty lines (containing nothing but a newline character) and whitespace-only lines (containing spaces or tabs followed by a newline). Both appear blank to the human eye, but:

  • Empty line: \n -- zero characters before the newline
  • Whitespace-only line: \n -- spaces/tabs before the newline (invisible but present)

Most "remove empty lines" tools handle both types. WritePadPro strips both -- any line with no visible characters is removed, whether it contains hidden whitespace or not. For detecting invisible characters that hide on seemingly blank lines, use the Invisible Character Detector.

Why Empty Lines Accumulate

Copy-Paste From Multiple Sources

Merging content from different documents, emails, and web pages accumulates blank lines at paste boundaries. Each source may end with trailing blank lines, and pasting them sequentially creates gaps that grow with each addition.

Code Editing and Refactoring

When developers delete blocks of code, they often leave behind the blank lines that originally surrounded the deleted block. After several rounds of editing, a file accumulates orphaned blank lines -- gaps where code used to be. Linting rules like ESLint's no-multiple-empty-lines catch this, but not every codebase enforces them.

Text Editor Auto-Formatting

Some editors insert extra blank lines when formatting code or text -- adding spacing around headings, between functions, or before comments. Auto-formatting applied multiple times can compound these insertions, doubling or tripling the spacing.

OCR and PDF Extraction

Text extracted from scanned documents often contains blank lines between every line of text -- one blank line for each gap between printed lines on the page. A single-spaced document becomes double-spaced after OCR, and a double-spaced document becomes quadruple-spaced.

Email Threading

Forwarded and replied emails accumulate blank lines between quoted sections. After 5-6 forwards, the email body may contain more blank lines than actual content. Extracting useful text from deeply threaded emails requires aggressive blank line removal. For related cleanup, see our Remove Line Breaks Guide.

Problems Caused by Excessive Empty Lines

In Code

Excessive blank lines in code files make navigation harder -- you scroll past empty space to find the next function. Many style guides limit blank lines: PEP 8 (Python) allows maximum 2 blank lines between top-level definitions and 1 between method definitions. ESLint's no-multiple-empty-lines defaults to a maximum of 2. Code review tools flag excessive spacing as a quality issue.

In Documents

Multiple blank lines between paragraphs create an unprofessional appearance -- it looks like the author used Enter key as a spacing tool instead of proper formatting (paragraph spacing, margins). In print, excessive blank lines waste paper. In digital documents, they force unnecessary scrolling.

In Data Files

Blank lines in CSV files create empty rows in spreadsheets. Blank lines in JSON or YAML can break parsers. Blank lines in configuration files may be interpreted differently by different applications -- some ignore them, others treat them as section separators or record delimiters. Clean data files should have no unintentional blank lines.

In Web Content

While HTML ignores blank lines in source code (collapsing all whitespace to single spaces), blank lines in content managed through plain text editors or CMS text areas can produce unexpected <br><br><br> sequences in rendered output, creating large gaps in the published page. For related cleanup, see our Remove Extra Spaces Guide.

Three Approaches to Empty Line Removal

1. Remove All Empty Lines

Deletes every line that contains no visible characters. The result has zero blank lines -- every line contains content.

When to use: Data cleanup, log processing, extracting content from heavily padded sources.

When NOT to use: Any context where blank lines serve as paragraph separators -- removing all of them collapses the entire text into one continuous block with no paragraph breaks.

2. Collapse Multiple Empty Lines to One

Replaces every run of 2+ consecutive blank lines with a single blank line. This normalizes spacing while preserving paragraph structure.

When to use: Document cleanup, code formatting, email text extraction -- the most common and safest approach for most content.

Regex: text.replace(/\n{3,}/g, "\n\n") -- replaces runs of 3+ newlines (which include 2+ blank lines) with exactly 2 newlines (one blank line).

3. Remove Only Whitespace-Only Lines

Removes lines that contain only spaces or tabs (not truly empty, but visually blank) while keeping intentionally empty lines. This is useful for cleaning up indentation artifacts without changing document structure.

Regex: text.replace(/^[ \t]+$/gm, "") -- matches lines containing only spaces or tabs and replaces them with truly empty lines (which can then be handled by approach 1 or 2). For removing duplicated content that creates these patterns, see our Remove Duplicate Lines Guide.

Empty Lines in Code: Style Guide Rules

Every major programming language has style conventions for blank line usage.

Language / GuideBetween Functions/ClassesWithin FunctionsMaximum Consecutive
Python (PEP 8)2 blank lines1 blank line (sparingly)2
JavaScript (ESLint)1-2 blank lines1 blank line2 (configurable)
Java (Google Style)1 blank line1 blank line (as needed)1
Go (gofmt)1 blank lineMinimal1 (enforced by formatter)
PHP (PSR-12)1 blank line1 blank line (as needed)1
Ruby (RuboCop)1 blank line0-1 blank lines1
CSS1 blank line between rulesetsNone within rules1

The Principle

Blank lines in code serve one purpose: visual separation between logical sections. One blank line between functions is helpful. Two blank lines between classes (Python) provide stronger separation. Three or more consecutive blank lines are never appropriate in any style guide -- they indicate deleted code, accidental insertions, or lazy formatting.

Programmatic Empty Line Removal

LanguageRemove All Empty LinesCollapse to Single
JavaScripttext.split("\n").filter(l => l.trim()).join("\n")text.replace(/\n{3,}/g, "\n\n")
Python"\n".join(l for l in text.split("\n") if l.strip())re.sub(r"\n{3,}", "\n\n", text)
PHPimplode("\n", array_filter(explode("\n", $text), "trim"))preg_replace("/\n{3,}/", "\n\n", $text)
Bashgrep -v "^$" file.txtcat -s file.txt
Sedsed "/^$/d" file.txtsed "/^$/N;/^\n$/d" file.txt

The cat -s command (squeeze) is the simplest way to collapse blank lines on Unix/Linux -- it replaces runs of blank lines with a single blank line.

Using WritePadPro's Remove Empty Lines Tool

WritePadPro's Remove Empty Lines tool strips blank lines from any text instantly.

Step 1: Paste Your Text

Open the Remove Empty Lines tool and paste your text with excessive blank lines -- from code files, documents, emails, data exports, or any source.

Step 2: Clean

Click the action button. The tool removes all lines that contain no visible characters, including lines with only whitespace (spaces, tabs). Multiple consecutive blank lines are collapsed.

Step 3: Review

The output shows clean text with no excessive gaps. The tool reports how many lines were removed so you can see the cleanup impact.

Step 4: Copy

Copy the clean text for use in your code editor, document, or data file.

Privacy

All processing runs locally in your browser. Your text -- including source code, configuration files, and data -- is never transmitted to any server.

Summary

Empty lines accumulate through copy-paste, code editing, auto-formatting, OCR extraction, and email threading. Excessive blank lines bloat files, violate code style guides, create unprofessional document appearance, and can break data parsers.

Three removal approaches serve different needs: remove all (for data cleanup), collapse to single (for most content -- the safest default), and remove whitespace-only lines (for code indentation artifacts). Most code style guides allow a maximum of 1-2 consecutive blank lines.

Strip empty lines with WritePadPro's Remove Empty Lines tool -- instant cleanup in your browser with complete privacy. For line break issues, see the Remove Line Breaks Guide. For spacing cleanup, see the Remove Extra Spaces Guide.

Frequently Asked Questions

What is the difference between an empty line and a whitespace-only line?

An empty line contains nothing but a line break character -- zero visible or invisible characters before the newline. A whitespace-only line contains spaces or tabs (invisible but present) followed by a line break. Both appear blank to the human eye, but they are technically different. Some tools only remove truly empty lines and miss whitespace-only lines, leaving invisible gaps. WritePadPro's Remove Empty Lines tool handles both types -- any line with no visible characters is removed, whether it contains hidden whitespace or not.

Should I remove all empty lines or just collapse them?

It depends on the context. For data cleanup (CSV, log files, extracted text), removing all empty lines is usually correct -- blank lines in data represent noise. For documents, code, and content where blank lines serve as paragraph or section separators, collapsing to single blank lines is safer -- it normalizes spacing while preserving structure. The regex for collapsing is: replace any run of 3+ consecutive newlines with exactly 2 newlines (which produces one blank line). This is the safest default for most text cleanup scenarios.

How many blank lines should code have between functions?

Most style guides recommend 1-2 blank lines between top-level functions or classes, and 0-1 blank lines within functions. Python's PEP 8 specifies 2 blank lines between top-level definitions and 1 between method definitions. JavaScript (ESLint), Java, Go, PHP, and Ruby style guides generally recommend 1 blank line between functions. No mainstream style guide allows 3 or more consecutive blank lines anywhere in code. Most linters and formatters enforce these limits automatically.

Can empty lines break data files?

Yes. In CSV files, blank lines create empty rows in spreadsheets that can cause errors in data processing formulas and import operations. In JSON, unexpected blank lines between structural elements can cause parser failures in strict-mode parsers. In YAML, blank lines change document structure -- they can separate YAML documents or create unexpected null values. In configuration files, some applications interpret blank lines as section separators, changing how settings are grouped. Always clean blank lines from data files before processing to avoid these issues.

Related Tools

Related Articles