Trim Whitespace

Remove leading and trailing whitespace from every line while leaving the characters between the ends of each line unchanged.

What this tool actually changes

Each input line is passed through JavaScript String.trim(). This removes whitespace at both ends, including common spaces, tabs, and other Unicode whitespace recognized by the browser.

Edge cases to know

Indentation is removed. That is useful for cleaning pasted lists but may be undesirable for source code where indentation is meaningful. Empty lines remain empty.

Example

Input:
  alpha
beta   
    gamma

Output:
alpha
beta
gamma

Good use cases

Pasted lists, CSV-like data, configuration values, copied documentation, and text where accidental indentation or trailing spaces should be removed.

Use only when outer whitespace is disposable

Trimming can make copied lists and values easier to compare, but it removes indentation from every line. In Python, YAML, Makefiles, Markdown code blocks, and other indentation-sensitive formats, that can alter meaning. Inspect a copy before using the result.

What this tool leaves alone

Characters between the first and last non-whitespace characters are untouched. Empty lines stay present, though any whitespace on those lines is removed. The implementation uses JavaScript trim(), so the set of trimmed characters follows the browser's definition of whitespace rather than a configurable ASCII-only rule.

Complexity and practical limit

The tool visits each line and holds the input and transformed output in the browser. For large source files or transformations that need to preserve encoding and line-ending details, use a local editor or file-aware script.

Frequently asked questions

Does it preserve indentation?

No. Leading whitespace is removed. Use this carefully with source code.

What whitespace is removed?

JavaScript trim() removes whitespace recognized by the browser at both ends of each line.

Does it remove blank lines?

No. Blank lines remain; use Remove Empty Lines if you want them removed.

Can it safely trim source code?

Not in general. Leading spaces can define blocks or continuation syntax. Use it only when you know outer whitespace is insignificant for the format you are editing.

Advertisements