Skip to content

Trim Whitespace

Strip the invisible mess around your text.

Whitespace problems are invisible until they break something. A trailing space on a line makes two otherwise identical entries compare as different. Leading indentation copied from a code block misaligns a list. Repeated blank lines pasted from a PDF turn a tidy document into a sparse one.

This tool removes that padding without touching the words. In its default mode it strips leading and trailing spaces and tabs from every line, which is the fix that solves most comparison and import problems.

Two other modes are available. Trailing only removes whitespace from the ends of lines but leaves indentation intact, which is what you want when the leading spaces are meaningful — code, poetry, or a formatted block. Aggressive mode goes further: it also collapses runs of spaces inside a line down to one and reduces long stretches of blank lines to a single blank line.

Examples

Examples for Trim Whitespace: each row pairs an input with the output it produces.
What it handles Input Output
Both ends hello world hello world
Aggressive mode a b c a b c
Trailing only keeps indentation indented indented

How it works

Line endings are normalized first so text from any operating system behaves identically. Each line then has trailing spaces and tabs removed, and — outside trailing-only mode — leading spaces and tabs as well. In aggressive mode two further passes run over the whole text: runs of two or more spaces or tabs inside a line collapse to a single space, and three or more consecutive line breaks collapse to two, leaving exactly one blank line between blocks. The text is then trimmed at both ends. Words, punctuation, and single spaces between words are never altered.

When people use it

  • Fixing a list where entries fail to match because of stray spaces.
  • Cleaning indentation copied out of a code editor or email.
  • Tightening text pasted from a PDF that arrived double-spaced.
  • Preparing data for import where trailing spaces would break keys.

Frequently asked questions

Will it remove the single spaces between my words?
Never. Only runs of two or more spaces are collapsed, and only in aggressive mode. Normal spacing between words is always preserved.
What is the difference from the paste cleaner?
This tool handles ordinary spaces, tabs, and blank lines. The paste cleaner additionally removes invisible Unicode characters and converts curly quotes, which is what you need for text from Word or a PDF.
Are tabs treated as whitespace?
Yes. Tabs are stripped from line ends alongside spaces, and collapsed in aggressive mode.