Trim whitespace from text

Trim whitespace from text by stripping leading or trailing spaces, tabs, and other whitespace from each line. Pick Both, Left Only, or Right Only, and choose whether to trim every line individually or treat the whole text as one block. The transform runs in your browser. To collapse runs of internal spaces too, follow with remove extra spaces.

Input
Line 1:1 LF cloud_done Saved locally
Result Trim Whitespace
0 lines 0 chars

How trimming works

JavaScript's built-in String.prototype.trim() strips every Unicode whitespace character from both ends of a string. That covers ASCII space, tab, line feed, carriage return, the non-breaking space (U+00A0), em space, en space, and the rest of the Unicode White_Space property. The internal whitespace of each line is left alone, only the runs at the very start or end disappear.

The Side dropdown swaps the regex used. Both calls trim() directly. Left Only uses /^\s+/ to delete only leading whitespace. Right Only uses /\s+$/ to delete only trailing whitespace. So you can clean up trailing spaces in code without touching indentation, or vice versa.

The Per Line toggle is on by default: each line is trimmed independently, so a multi-line block has every line cleaned. Turn it off and the trim runs once on the whole string, which only strips the very first and very last runs of whitespace in the document. Both modes preserve the line endings between lines exactly as they came in.

How to use trim whitespace from text

  1. 1Paste your text into the input panel on the left.
  2. 2Pick a Side: Both, Left Only, or Right Only.
  3. 3Leave Per Line on to clean every line, or turn it off to trim the document's ends only.
  4. 4Read the trimmed result on the right as you type.
  5. 5Click Copy to grab the cleaned text.

Keyboard shortcuts

Drive TextResult without touching the mouse.

Shortcut Action
Ctrl FOpen the find & replace panel inside the input Plus
Ctrl ZUndo the last input change
Ctrl Shift ZRedo
Ctrl Shift EnterToggle fullscreen focus on the editor Plus
EscClose find & replace, or exit fullscreen
Ctrl KOpen the command palette to jump to any tool Plus
Ctrl SSave current workflow draft Plus
Ctrl PRun a saved workflow Plus

What this tool actually does

Trims every Unicode whitespace character

Built on String.prototype.trim() and the \s regex class, which match every codepoint with the Unicode White_Space property. ASCII space and tab, non-breaking space, em space, line tabulator, and others are all caught.

Choose left, right, or both ends

The Side dropdown selects the regex. Left Only deletes leading whitespace only, useful for stripping outdented code. Right Only deletes trailing whitespace only, useful for cleaning up source files before commit. Both does the standard double-sided trim.

Per-line versus whole-text mode

With Per Line on (default), the input is split on \n or \r\n, each line is trimmed, then the result is rejoined with \n. With it off, the trim runs once on the whole string, which only touches the very first and very last whitespace runs.

Internal whitespace is untouched

Spaces and tabs inside each line stay in place. The trim only matches at the edges of each line (or each whole document, if per-line is off). Use remove extra spaces if you also want to collapse runs in the middle.

Blank lines pass through

A line containing only whitespace becomes an empty line, but the line itself is still in the output. To also drop those rows, follow with remove blank lines, which can be set to treat whitespace-only lines as empty.

Worked example

Side: Both, Per Line on. Leading and trailing whitespace on every line is stripped, including the leading tab on row two.

Input
   indented line one   
	line with tab
  spaced both sides  
Output
indented line one
line with tab
spaced both sides

Settings reference

Setting or behaviour Effect on output
Side: Both (default) Trims leading and trailing whitespace on each line.
Side: Left Only Trims leading whitespace only. Trailing spaces and tabs survive.
Side: Right Only Trims trailing whitespace only. Indentation survives.
Per Line on (default) Splits on newline, trims each line, rejoins. Cleans every row.
Per Line off Trims the whole text once. Only the first and last whitespace runs in the document are removed.
Internal whitespace Always preserved. Use remove extra spaces to collapse runs in the middle.
Blank lines Stay in the output (as empty rows). Use remove blank lines to drop them.

FAQ

Will it remove tabs too, or just spaces?
Both. The trim uses the regex class \s, which matches every Unicode whitespace character including ASCII space, tab, line feed, carriage return, non-breaking space, and em space. Anything the language considers whitespace is removed at the line edges.
How is per-line different from one-shot trim?
With Per Line on, every row is cleaned individually, so an indented middle line loses its leading spaces. Turn it off and only the very first and very last whitespace runs in the whole document disappear, which is what you want when middle indentation should survive.
Does it remove empty lines?
No. A line of only spaces becomes an empty line, but the empty line itself stays. To drop those rows, run the result through remove blank lines with the whitespace-empty toggle on.
Will internal spaces collapse too?
No. Trim only acts on the edges of each line. Spaces and tabs inside a line stay in place. To squeeze runs of internal spaces, follow with remove extra spaces.
Does it modify line endings?
Lines split on either \n or \r\n, but the rejoin uses \n. So a CRLF input comes out as LF after a per-line trim. Run find and replace afterwards if you need to restore CRLF.