What counts as whitespace
Whitespace is any character that shows up as empty space: the normal space, the tab, the line break, and a group of less familiar characters such as the no-break space (U+00A0), the em space (U+2003) and the zero width space (U+200B). They are invisible, so they are easy to miss, but they cause real problems. Double spaces look sloppy in print. Trailing spaces break spreadsheet lookups. A zero width space hidden in a product code makes two codes that look identical fail to match.
This tool finds that whitespace and removes or normalizes it, while leaving your words alone.
How to use the whitespace remover
- Paste your text into Your text.
- Look at Clean text on the right. It updates as you type or change an option.
- Choose what to clean. These options are on by default:
- Trim line starts and ends removes spaces and tabs at the beginning and end of every line.
- Double spaces to single turns any run of spaces into one space.
- Remove blank lines deletes empty lines, including lines that only contain spaces.
- Fix hidden and odd spaces turns no-break spaces and other wide spaces into normal ones and deletes zero width characters.
- These are off by default, for bigger changes:
- Tabs to spaces replaces each tab with a single space.
- Remove all line breaks joins every line into one, with a space in between.
- Remove all spaces deletes every space and tab.
- Press Copy or Download.
The note under the tool tells you how many whitespace characters were removed.
Examples
| You paste | Options | You get |
|---|---|---|
Too many spaces here. |
Default | Too many spaces here. |
| Line one, three empty lines, Line two | Default | Line one, then Line two on the next line |
a b c |
Remove all spaces | abc |
| one, two and three on separate lines | Remove all line breaks | one two three |
4111 1111 1111 1111 |
Remove all spaces | 4111111111111111 |
| Tabs[tab]and, where [tab] is a tab | Tabs to spaces | Tabs and |
In the first example the tool removed 12 whitespace characters. A line such as “and hidden spaces” copied from a web page might contain a no-break space and a zero width space. Fix hidden and odd spaces turns the first into a normal space and deletes the second, so the words that were glued together by the invisible character end up next to each other, exactly as they appeared on screen.
Why extra whitespace appears
- Copying from PDFs. PDF text is placed line by line, so copying often brings line breaks and runs of spaces with it. If the problem is mostly line breaks in the middle of sentences, the line break remover is the better fit.
- Copying from web pages. Many sites use no-break spaces to keep words together, and some insert zero width spaces to allow line wrapping in long words.
- Typing habits. Two spaces after a full stop is still common. Most style guides now ask for one.
- Exports from other programs. Spreadsheets and databases often pad values with trailing spaces to a fixed width.
- Indentation. Text copied from code or emails can come with tabs and leading spaces on every line.
Tips and limits
- Keep paragraphs but lose the mess. Switch off Remove blank lines if your text uses blank lines between paragraphs. You still get trimmed lines and single spaces.
- Tables and columns. Text copied from a spreadsheet uses tabs between cells. Leave Tabs to spaces off if you want to paste the result back into a spreadsheet.
- Code. Indentation matters in Python, YAML and Makefiles. Switch off Trim line starts and ends before cleaning code, or the indentation will be removed.
- Other symbols. This tool only touches whitespace. To remove punctuation and symbols too, use the special character remover. For fancy fonts and curly quotes, use the plain text converter.
Other ways to remove extra spaces
Microsoft Word. Open Find and Replace (Ctrl+H). Put two spaces in Find what and one space in Replace with, then press Replace All. Repeat until Word reports no more replacements. To remove empty lines, search for ^p^p and replace with ^p.
Google Docs. Open Edit, then Find and replace. Tick Use regular expressions, find {2,} (a space followed by {2,}) and replace it with a single space.
Excel and Google Sheets. =TRIM(A1) removes spaces at both ends and turns runs of spaces inside the text into one. It does not remove no-break spaces. Wrap it like this for text from the web: =TRIM(SUBSTITUTE(A1, CHAR(160), " ")).
Code. In JavaScript, text.replace(/ {2,}/g, ' ').trim() collapses spaces, and text.replace(/\s+/g, '') removes all whitespace. In Python, " ".join(text.split()) collapses every run of whitespace, including line breaks, into a single space.