Remove Extra Spaces Without Losing Line Breaks
Double spaces after periods, tabs from copied tables and indentation from PDFs make text look messy. This tool will remove extra spaces so that words are separated by exactly one space, while your line breaks and paragraphs stay where they are.
What gets cleaned
- Runs of spaces, tabs and non-breaking spaces inside a line become one ordinary space.
- Spaces at the start and end of every line are trimmed. Lines that held only spaces become truly empty.
- With the blank-line option on (the default), three or more line breaks in a row shrink to two, which leaves at most one blank line between paragraphs.
Example
Before:
" This sentence has[tab][tab]too many spaces. "
""
""
""
" And so does this one."
After:
"This sentence has too many spaces."
""
"And so does this one."
Turn the blank-line option off and all four line breaks between the two sentences are kept; only the spaces are fixed.
Remove extra spaces vs remove all whitespace
This tool keeps a single space between words, so the text stays readable. If you need every space gone, for example to join a hex string or a code that was typed in groups, use Remove Whitespace. If the problem is line breaks in the middle of sentences, Remove Line Breaks is the better fit.
Things to watch out for
- Indented code. Leading indentation is trimmed, so Python or YAML loses its structure. Use a code formatter for source code instead.
- Tab-separated data. Tabs become spaces, which merges TSV columns. Convert the data before cleaning it.
- Invisible characters. Zero-width spaces and soft hyphens are not whitespace, so they are left in place. The Text Cleaner removes those too.
- Intentional alignment. Text aligned with spaces, such as an ASCII table, will lose its columns.
The same cleanup in spreadsheets and code
Excel and Google Sheets have a built-in TRIM() function that removes leading and trailing spaces and collapses runs of spaces inside a cell. It does not touch non-breaking spaces (CHAR(160)), which often arrive with text copied from web pages, so combine it with a substitution: =TRIM(SUBSTITUTE(A1, CHAR(160), " ")).
In JavaScript, this tool's behavior for one block of text looks roughly like this:
text.split("\n")
.map(line => line.replace(/[ \t\u00A0]+/g, " ").trim())
.join("\n")
.replace(/\n{3,}/g, "\n\n");In code editors, a regular-expression search for {2,} replaced with a single space fixes double spaces without touching line breaks.
Why double spaces happen
Typewriter habit (two spaces after a period), justified text copied from PDFs, and HTML where several spaces were used for layout are the usual sources. Most style guides today use a single space after a period, and web browsers collapse extra spaces anyway, so extra spaces mostly cause trouble in plain-text fields, databases and word counts.
Privacy
All cleaning happens in your browser; your text is not sent to a server.