Guide

How to Remove Extra Spaces and Clean Up Pasted Text

Written by Alex Johnson  ·  6 min read  ·  Updated November 2026  ·  Virtual Text Tools

Whitespace Cleaner

Six cleaning modes. Use Swap to chain them.
Input
Output

Runs entirely in your browser. Your text is never uploaded. No limits, no signup.

Quick answer: paste your text into the cleaner above and press Remove extra spaces. That collapses every run of spaces, tabs or non-breaking spaces into one normal space and trims both ends of each line. For other problems, pick the matching mode below; you can run several in a row with the Swap button.

Messy spacing almost always comes from copying text out of something that was never meant to be copied from. PDFs are the worst offenders. A PDF stores characters at fixed positions on a page, not as a stream of words, so when you copy from one, the viewer has to guess where the spaces and line breaks go. It often guesses an extra space between words, a hard line break at the end of every visual line, and a blank line wherever two blocks of text sat apart on the page. Web pages contribute non-breaking spaces. Spreadsheets and database exports pad fields with trailing spaces so the columns line up.

None of this is visible in a normal editor, which is why it is annoying. Two spaces look like one at reading size, and a trailing space looks like nothing at all, until a search fails or a line of code is flagged in review.

The six modes, and what each one actually does

The six whitespace cleaning modes with before and after examples: remove all spaces, remove extra spaces, remove leading spaces, remove trailing spaces, remove blank lines, and tabs to spaces

Remove extra spaces

The one to use for pasted prose. On each line it replaces any run of whitespace (spaces, tabs, non-breaking spaces) with a single ordinary space, then trims the start and end of the line. Hello   world becomes Hello world. Line breaks are left where they are.

Because it trims the start of every line, this mode also removes indentation. Do not run it on code or on a nested list you want to keep nested; use Remove trailing spaces instead.

Remove trailing spaces

Strips whitespace from the end of each line and touches nothing else. This is the safe choice for code and config files. Python's style guide, PEP 8, tells you to avoid trailing whitespace, many linters can be set to flag it, and Git highlights it in coloured diffs, so stray trailing spaces tend to turn into review comments.

Remove leading spaces

Strips whitespace from the start of each line only. Useful when text copied from an indented block, an email reply or a Word list arrives with every line pushed to the right. Spacing inside the line is untouched.

Remove blank lines

Deletes every empty line, including lines that only contain spaces. Be aware that this removes the gaps between paragraphs too, so a five-paragraph text comes out as one continuous block of lines. That is what you want for a list or a data export, and usually not what you want for an article.

If your real problem is a line break in the middle of every sentence, which is typical of PDF copies, this is the wrong tool. Use remove line breaks, which joins the lines inside each paragraph and keeps the breaks between paragraphs.

Tabs to spaces

Replaces every tab character with four spaces. That is right for indentation at the start of a line, which is where tabs usually cause trouble: PEP 8 prefers spaces for Python indentation, and mixing the two in one Python file can raise a TabError. It is not right for tabs used to line up columns in the middle of a line. A real tab jumps to the next tab stop, so its width varies; replacing it with a fixed four spaces will knock those columns out of alignment. Go is the notable exception in the other direction: its formatter, gofmt, indents with tabs, so leave Go code alone.

Remove all spaces

Deletes every space, tab and non-breaking space while keeping line breaks. Hello World Test becomes HelloWorldTest. It is rarely what you want for readable text. It is useful for cleaning things that should never contain spaces in the first place: a phone number pasted as 555 014 2368, an IBAN or card-style number broken into groups, or a hex string copied from a formatted document.

Fixing a typical PDF paste

Copied text from a PDF report often looks like this, with a double space after some words, trailing spaces you cannot see and a blank line between every printed line:

Quarterly  revenue  rose  4%, driven by

renewals in the  education segment and

a smaller increase in new accounts.

Fix it in three passes. Press Remove extra spaces to collapse the doubled spaces. Press Swap to move the result back into the input, then press Remove blank lines so the three fragments sit on consecutive lines. Finally paste that into remove line breaks to join them into one paragraph:

Quarterly revenue rose 4%, driven by renewals in the education segment and a smaller increase in new accounts.

The middle step matters. A line-break joiner that keeps paragraphs treats a blank line as the end of a paragraph, so if the blank lines are still there it will give you back three separate fragments.

Non-breaking spaces

The non-breaking space (Unicode U+00A0) looks exactly like a normal space but is a different character. Browsers and word processors use it to stop two words being split across lines, as in "10 kg" or "Mr. Smith", and it comes along whenever you copy text from a web page that contains  . In a plain-text file it causes two kinds of trouble. Searches and comparisons fail, because "New York" with a non-breaking space does not equal "New York" with a normal one. And some cleanup functions skip it: Excel's TRIM only removes the ordinary space character (code 32), so cells with non-breaking spaces still look padded after trimming.

Remove extra spaces, Remove leading spaces, Remove trailing spaces and Remove all spaces in this tool all treat non-breaking spaces as whitespace. Zero-width spaces (U+200B) are a different matter: they are not counted as whitespace by JavaScript, so they survive. If two lines still refuse to match after cleaning, a zero-width space is the likely cause.

Doing the same thing elsewhere

Clean a block of text now

No account and no upload. The cleaner is at the top of this page.

↑ Back to the tool

Frequently asked questions

How do I remove double spaces from text?

Paste the text into the cleaner at the top of this page and press Remove extra spaces. Any run of two or more spaces becomes one space, and the start and end of each line are trimmed.

Why does text copied from a PDF have extra spaces?

A PDF stores characters at positions on a page rather than as continuous text. When you copy from it, the viewer rebuilds the spacing from those positions and often adds extra spaces, hard line breaks and blank lines.

How do I remove blank lines but keep paragraphs?

Remove blank lines deletes every empty line, including paragraph gaps. If you want to keep paragraphs and only fix lines broken mid-sentence, use the remove line breaks tool, which keeps the breaks between paragraphs.

Will Tabs to spaces keep my columns aligned?

It keeps indentation at the start of lines consistent, but tabs used to line up columns in the middle of a line can shift, because each tab becomes exactly four spaces instead of jumping to the next tab stop.

Is my text sent to a server?

No. The cleaner runs as JavaScript in your browser tab. Your text never leaves your device.

AJ
Alex Johnson
Developer & Founder, Virtual Text Tools
Alex Johnson is a developer and the founder of Virtual Text Tools. After years of being forced to create accounts just to count words or clean up a list, he built a suite of free, browser-based text utilities that work instantly with no signup required. He writes about productivity tools, web development, and practical text manipulation techniques.