How to Remove Extra Spaces and Tabs From Text

Extra whitespace comes in several kinds, and each has its own fix: collapse repeated spaces between words to one, trim spaces from the ends of lines, strip indentation only when it is not doing a job, and decide whether tabs should stay tabs. To remove extra spaces from text without damaging it, handle these separately instead of deleting all whitespace in one sweep.

In the Text Cleaner, the default General Clean preset already collapses repeated spaces inside lines and trims trailing spaces. Indentation and tabs are left alone until you tick the options for them.

The kinds of extra whitespace

Kind Where it comes from Option that removes it
Repeated spaces between words Justified text copied from a PDF, columns lined up with the space bar, edits that left two spaces behind Collapse multiple spaces
Two spaces after a full stop (period) A typing habit that dates from typewriters Collapse multiple spaces
Leading indentation Quoted or forwarded email, nested lists, first-line indents, centred text Trim each line
Trailing spaces Fixed-width exports padded to a set width, editors, Markdown line breaks Trim line endings
Tabs Cells copied from a spreadsheet, tab stops in a document, code Tabs → spaces
Whitespace-only lines Blank lines that an editor indented, empty table cells Trim line endings, which empties them
Blank space around the whole text Selecting a little too much when copying Trim entire text

A before and after

In these samples · stands for a space and [TAB] for a tab character.

Before

··Meeting·notes··
·
Budget:··approved.··Next·review·in·May.·
[TAB]Owner:[TAB]Priya···
····Venue:·Room·4

After (General Clean, the default)

Meeting·notes

Budget:·approved.·Next·review·in·May.
[TAB]Owner:[TAB]Priya
····Venue:·Room·4

Trailing spaces are gone, double spaces are single, the line that held one space is now empty, and the start of the text is trimmed. The tabs and the four-space indent are untouched, because General Clean does not remove indentation or convert tabs.

After (with Trim each line and Tabs → spaces also ticked)

Meeting·notes

Budget:·approved.·Next·review·in·May.
Owner:·Priya
Venue:·Room·4

The Whitespace options in the Text Cleaner

The common switches are in the Whitespace card under the Input Text box; the rest are in the Whitespace group under Advanced Cleaning Options.

  • Collapse multiple spaces reduces any run of spaces inside a line to one. That covers accidental doubles and the two-spaces-after-a-full-stop habit alike. Leading indentation is left as it is.
  • Trim line endings removes spaces and tabs from the end of every line. You cannot see them, but they stop two lines from matching, which matters when you are removing duplicate lines.
  • Trim each line removes spaces and tabs from both ends of every line, so it strips indentation as well.
  • Tabs → spaces replaces each tab with one space. It does not expand a tab to four or eight columns, so tab-aligned text will not stay aligned. Tabs are converted before spaces are collapsed, so with both options ticked a run of tabs ends up as a single space.
  • Trim entire text removes spaces and blank lines before the first character and after the last one. A tab at the very start is kept, so the first row of tab-separated data does not shift.
  • Collapse multiple blank lines to one keeps paragraph gaps and shrinks larger ones. Remove empty lines deletes every blank line, which suits lists and not prose.
  • Normalize non-breaking spaces turns look-alike space characters into ordinary ones; more on that below.

Tick what you need and press Clean Text. After that first clean, changing an option updates the result without another press, except on very long texts. The summary reports what changed; for the sample above it includes 2 extra spaces removed.

Doing it without a tool

In Word. Open Find and Replace, type two spaces in Find and one in Replace, and press Replace All until Word reports no more replacements. With Use wildcards ticked, one pass is enough: find {2,} (the pattern begins with a space, and {2,} means at least two of it) and replace it with one space.

In Google Docs. The same two-spaces-to-one replacement works in Find and replace, repeated until nothing is left. The dialog also has a Match using regular expressions option.

In a code editor. Switch the search box to regular-expression mode:

Goal Find Replace with
Collapse repeated spaces {2,} (starts with a space) One space
Remove trailing whitespace [ \t]+$ Nothing
Remove leading whitespace ^[ \t]+ Nothing
Convert tabs \t One space

Avoid the shortcut of replacing \s+ with a space. \s also matches line breaks, so it joins every line of the text into one.

In Excel. TRIM removes leading and trailing spaces and reduces runs between words to one. It was designed for the ordinary space character only, so a non-breaking space survives it. For imports and lookups, see cleaning text for spreadsheets and databases.

When a space refuses to collapse

If a double space survives every replacement, one of the two is probably not a space. The usual suspect is the non-breaking space, U+00A0, which looks identical and comes from web pages, word processors and PDFs. A search for an ordinary space does not match it.

Before

Total:·[NBSP]40·kg

After (Normalize non-breaking spaces with Collapse multiple spaces)

Total:·40·kg

The option converts U+00A0, the narrow no-break space (U+202F) and the figure space (U+2007) to ordinary spaces, and those then collapse like any others. Both options are part of General Clean. Non-breaking spaces explained covers where the character comes from and why it defeats trimming.

Other Unicode spaces, such as the em space (U+2003) and the thin space (U+2009), are not converted by that option. Paste one into Find & Replace to swap it for an ordinary space. And when a word will not match in a search although nothing looks wrong, suspect a zero-width character instead; see invisible Unicode characters.

When not to remove whitespace

Sometimes the whitespace is the structure, and cleaning it changes what the text means.

  • Code. Python uses indentation to define blocks, YAML uses it for nesting, and a Makefile needs a real tab before each command. Collapse multiple spaces leaves leading indentation alone but still changes spacing inside a line, including inside string literals. Use the language’s own formatter for code.
  • Markdown hard line breaks. Two spaces at the end of a line tell Markdown to break the line there. Trim line endings removes them, and the lines run together when rendered. Markdown also relies on leading spaces for nested lists and indented code blocks, which Trim each line would flatten.
  • Tab-separated data. In tab-separated text, which is what cells copied from a spreadsheet usually become, the tab is the column boundary and two tabs in a row mark an empty cell. Convert tabs to spaces and the boundaries are gone, most obviously when a value contains a space of its own. Trim line endings, which the default preset includes, also removes tabs at the end of a line, and in a row those are empty trailing cells.
  • Aligned plain-text tables. Columns lined up with spaces depend on every one of them, as the next sample shows.

Before

Item········Qty···Price
Notebook······2····4.50
Pen··········10····0.80

After (Collapse multiple spaces)

Item·Qty·Price
Notebook·2·4.50
Pen·10·0.80

The words are intact and the table is not. For text like this, untick Collapse multiple spaces and keep only Trim line endings, or leave the text alone.

Sources

  1. CommonMark Spec, Version 0.31.2 — CommonMark
  2. TRIM function — Microsoft Support
  3. General Punctuation (Range: 2000–206F) — Unicode Consortium

This guide was drafted with AI assistance and reviewed by the publisher. See the editorial policy for how guides are written and corrected.