How to Remove Extra Spaces from Text (Excel, Word, Notepad++ and Online)

· 5 minutes read

How to Remove Extra Spaces from Text (Excel, Word, Notepad++ and Online)

Text copied from a PDF, a website or an exported report rarely arrives clean. There are spaces at the end of lines, two or three spaces between words, tabs instead of spaces, and sometimes characters you cannot even see. They look harmless, but they make VLOOKUP fail, put "Smith " and "Smith" into two different groups and break CSV imports.

This guide shows how to remove extra spaces from text with the tool you already have open – and why the usual quick fixes sometimes leave a space behind.

The fastest way: trim text online

  1. Open the Trim text tool.
  2. Paste your text or open a .txt / .csv file.
  3. Keep the default options: trim line starts and ends, collapse multiple spaces, fix non-breaking and zero-width spaces, and trim the whole text.
  4. Copy or download the result.

Each line is cleaned separately, so line breaks stay where they were. If you are cleaning code or an indented list, turn off Trim line starts and Collapse multiple spaces – only the trailing spaces will go. The tool can also shorten text to a fixed number of characters or words, which is handy for meta descriptions and social posts.

Everything runs in your browser, so you can safely paste customer lists or internal documents.

How to remove extra spaces in Excel and Google Sheets

Both programs have the TRIM function:

=TRIM(A1)

It removes spaces at the start and end of the cell and turns runs of spaces between words into a single space. Put the formula in a helper column, fill it down, then copy the column and use Paste special → Values to replace the original data.

The catch: TRIM only removes the normal space (character 32). Text from websites often contains the non-breaking space (character 160), which TRIM ignores. Replace it first:

=TRIM(SUBSTITUTE(A1, CHAR(160), " "))

To also remove line breaks and other non-printable control characters (codes 0–31) inside a cell, wrap it in CLEAN:

=TRIM(CLEAN(SUBSTITUTE(A1, CHAR(160), " ")))

In Excel versions with a semicolon list separator, use ; instead of , between arguments.

How to remove double spaces in Microsoft Word

The simple way is Find and Replace (Ctrl+H): search for two spaces, replace with one, and press Replace All repeatedly until Word reports zero replacements.

The one-step way uses wildcards:

  1. Press Ctrl+H and click More.
  2. Tick Use wildcards.
  3. Find what: ( ){2,}
  4. Replace with: \1
  5. Click Replace All.

( ){2,} means "a space, two or more times", and \1 puts back a single copy of it. Word can also find non-breaking spaces: without wildcards, search for ^s.

How to trim whitespace in Notepad++ and VS Code

Notepad++ has it built in: Edit → Blank Operations → Trim Leading and Trailing Space. To collapse double spaces, open Replace (Ctrl+H), choose Regular expression as search mode and use:

  • Find: {2,} (a space followed by {2,})
  • Replace: a single space

In VS Code, press Ctrl+H, enable regex (Alt+R) and replace [ \t]+$ with nothing to remove trailing whitespace. You can also turn on the files.trimTrailingWhitespace setting so it happens on every save.

How to remove extra spaces on the command line

With sed (works on Linux and macOS):

sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//; s/ {2,}/ /g' input.txt > output.txt

The three commands remove leading whitespace, trailing whitespace and double spaces on every line.

In Python, " ".join(text.split()) is a popular one-liner, but be careful – it also joins all lines into one, because split() treats line breaks as whitespace. To keep the lines:

cleaned = "\n".join(" ".join(line.split()) for line in text.splitlines())

Common pitfalls

  • Non-breaking spaces. They look like spaces but are a different character (U+00A0). Most trim functions ignore them. If a space "cannot be deleted", this is usually the reason.
  • Zero-width characters. The zero-width space (U+200B) and the byte order mark (U+FEFF) are invisible but still count as characters, so two visually identical words do not match. Our tool removes them; in Excel you would need SUBSTITUTE(A1, UNICHAR(8203), "").
  • Indentation. Trimming line starts in code, YAML or Markdown lists destroys the structure. Trim only the ends there.
  • Tabs. Data copied from spreadsheets is often tab-separated. Replacing tabs with spaces merges the columns, so keep tabs if you are going to paste the text back into a spreadsheet.

FAQ

What is the difference between trimming and removing all spaces?

Trimming removes only the unnecessary spaces – at the edges and the duplicates between words. Removing all spaces deletes every space, so "John Smith" becomes "JohnSmith". For that, use the spaces remover.

Why does TRIM not work on my data?

Almost always because of non-breaking spaces (character 160) or other invisible characters. Use the SUBSTITUTE formula above, or paste the column into the Trim text tool, which handles them automatically.

How do I remove empty lines as well?

Turn on Remove empty lines in the Trim text tool, or use the dedicated empty lines remover.

Can I replace spaces with something else?

Yes – use find and replace, for example to replace every space with an underscore or a comma.