Trim whitespace — clean extra spaces in each line

Trim leading and trailing whitespace on every line and collapse repeated spaces or tabs to one space. Keep line breaks and preview the cleaned text.

Output
Enter text to see output.

What to know

For each line separately, this operation removes whitespace at the start and the end, then replaces any run of two or more spaces or tabs in the middle with a single space. The line count never changes, so blank lines remain blank and the layout of the list stays intact.

Take two lines: three spaces, hello, four spaces, world, two trailing spaces, then a tab, foo, two tabs and bar. The output is hello world on the first line and foo bar on the second. A single tab between words would stay a tab, since only runs of two or more are squeezed.

The edge trimming removes every kind of whitespace JavaScript recognises, including non-breaking spaces and the full-width ideographic space, which is handy for text copied from web pages and East Asian documents. The inner squeezing, however, only touches ordinary spaces and tabs, so a doubled non-breaking space in the middle is left alone.

Typical jobs are text copied from a PDF with ragged spacing, CSV fields padded with spaces and form input that should be normalised before comparing. If you also want blank lines gone, follow up with remove empty lines in a second pass.

Worked example

Use this for pasted labels, lists and prose with accidental spacing. It handles each line separately, so the number of rows stays the same.

Input

  alpha   beta  
  gamma  

Output

alpha beta
gamma

Indentation is removed. Do not use this operation on whitespace-sensitive source code or tab-separated tables when spacing carries meaning.

Choose a specific task 10
Related tools 4
How to use Trim whitespace — clean extra spaces in each line
  1. Paste text with ragged spacing, for instance a PDF extract where words are separated by 3 or 4 spaces.
  2. Read the Output panel and check that each line starts at the left edge and has single spaces between words.
  3. Look for any blank lines that remain, since this operation keeps them, and run remove empty lines next if they should go.
  4. Copy the cleaned text into your form, spreadsheet or document with the copy icon.

Quick answers

Does trimming delete blank lines?

No. A line that is only spaces becomes an empty line but is not removed, so the number of lines stays the same.

Why is a double space inside a line collapsed but a single tab is not?

Only runs of two or more spaces or tabs are squeezed to one space. A lone tab between words is left as it is.

Are full-width spaces at the ends of Japanese lines removed?

Yes, edge trimming removes the ideographic space as well as ordinary and non-breaking spaces. Doubled full-width spaces in the middle of a line are not squeezed.

Will it join lines that were wrapped by a PDF?

No, each line stays separate. To merge wrapped lines into a paragraph, use remove line breaks instead.