Pasted text is rarely clean. It arrives with double spaces after sentences, trailing tabs, blank lines between paragraphs, and line breaks that do not match the destination. Cleaning it by hand is slow and inconsistent; recognising the invisible characters makes the job mechanical.
The characters you cannot see
| Character | Often written as | What it does |
|---|---|---|
| Line feed | \n | Starts a new line (Unix, macOS). |
| Carriage return + line feed | \r\n | Starts a new line (Windows). |
| Carriage return | \r | Old Mac line ending. |
| Tab | \t | Inserts a wide gap or column spacing. |
| Space | ordinary space | A single blank. |
| Non-breaking space | / U+00A0 | A space that refuses to wrap. |
The last one is the trap. A non-breaking space looks identical to a normal space but is a different character, so a search for " " (space) will not find it and some cleanup tools leave it behind.
Before and after
Suppose the pasted text is:
Hello world.
This is a line
with blank rows.
After collapsing runs of spaces to one, trimming each line, and removing blank lines, it becomes:
Hello world.
This is a line
with blank rows.
Three separate operations did that: trim (strip leading and trailing whitespace per line), collapse (turn two or more spaces or tabs into one), and remove empty lines. Running them in that order gives a predictable result.
A safe cleaning recipe
- Normalise line endings first. Convert every
\r\nand\rto\nso later steps see one kind of break. - Replace non-breaking spaces. Swap U+00A0 for a normal space before collapsing, or it will survive.
- Collapse spaces and tabs. A single pass turning any run of whitespace within a line into one space.
- Trim each line. Remove the space left at the start or end.
- Remove blank lines if you want a compact block, or keep single blank lines if paragraphs must stay separated.
When to remove breaks and when to keep them
Removing every line break turns a list into one longline, which is useful when a field expects a single value (a spreadsheet cell, an address line, an API parameter). It is harmful when the breaks carry meaning: poetry, code, CSV rows and step-by-step lists all lose structure. Decide from the destination, not the habit. If paragraphs matter, keep one blank line between them and remove the rest.
Common mistakes
- Removing breaks before collapsing spaces. The collapsed line can then glue two logical lines together.
- Ignoring tabs. A tab is whitespace but many "remove spaces" buttons only target the space character.
- Missing non-breaking spaces. They hide in text copied from websites and word processors.
- Over-cleaning CSV or code. Stripping indentation or row breaks breaks the data.
Paste your text into our remove duplicate lines tool to trim, collapse and de-duplicate in one pass. To check exactly what changed between the messy original and the cleaned version, run both through the text comparison guide. If the text is bound for a structured format, the CSV to JSON guide explains how line breaks behave there.