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)

Many "remove empty lines" tools handle both types. WritePadPro’s default mode removes both — any line with no visible characters goes, whether it contains spaces and tabs or not. Its Strict mode removes only truly empty lines and keeps whitespace-only ones. 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 sometimes contains a blank line after every line of text, so a single-spaced page can come out looking double-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 can create empty rows in spreadsheets. Standard JSON ignores blank lines between values, but line-based formats such as JSON Lines expect one record per line, and some readers reject empty ones. 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

Most programming languages have style conventions for blank line usage. The table summarizes common ones; exact limits depend on the guide and on how your linter or formatter is configured.

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)Several allowed, not encouraged
Go (gofmt)1 blank lineMinimal1 (enforced by formatter)
PHP (PSR-12)1 blank line1 blank line (as needed)Not specified
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 rarely appropriate — they usually point to deleted code or accidental insertions.

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: Choose a Mode and Clean

Pick a mode from the Mode menu: Remove all blank lines (including whitespace-only), the default, which matches approach 1 above; Consolidate: keep one blank line between blocks, which matches approach 2 and also drops blank lines at the very start and end; or Strict: remove only zero-length lines, which keeps lines that contain only spaces or tabs. Then click Remove Empty Lines.

Step 3: Review

As you type, a live panel shows Total Lines, Blank Lines, Content Lines and how many lines Will Be Removed in the chosen mode. After you click the button, the cleaned text replaces your input and a summary reports how many lines were removed and how many remain.

Step 4: Copy or Download

Use Copy Result or Download .txt to take the clean text to your code editor, document, or data file. Undo brings back the original.

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). Many code style guides and linters limit consecutive blank lines to 1 or 2.

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 in its default mode — any line with no visible characters is removed, whether it contains hidden whitespace or not. Its Strict mode removes only truly empty lines.

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. In WritePadPro's Remove Empty Lines tool, the Consolidate mode does this for you.

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. Few style guides encourage 3 or more consecutive blank lines, and many linters and formatters can enforce a limit 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. Standard JSON ignores whitespace, including blank lines, between values, but line-based formats such as JSON Lines expect one record per line, and some readers reject empty lines. In YAML, blank lines are usually ignored, but inside multi-line block text they are kept as part of the value. 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.

Try These Tools

Related Articles