Trim spaces and tabs from the start or end of every line

Paste text and every line loses the spaces and tabs at its edges; choose both ends, the start only, or the end only.

Live demo · real result from this tool
Trim

Result

indented line trailing spaces tabbed line

Processed on your device — nothing is uploaded.

How to use Trim Lines

  1. Paste your text into Your text.
  2. Pick what to Trim: Both ends, Start only or End only.
  3. Copy the cleaned text from the result.

What it does and when to use it

Invisible whitespace at the edges of lines causes real problems. Two names that look the same do not match in a spreadsheet lookup because one has a trailing space. A list pasted from a web page is pushed to the right by leading tabs. Code review tools flag every line that ends in spaces.

Choose the option for the job:

  • Both ends is for lists and data: names, email addresses, product codes, IDs. Each value ends up tight, so sorting and matching work as expected.
  • Start only removes indentation, for example from a block quote or a text copied out of a nested list, while leaving line endings alone.
  • End only is for code, Markdown and config files. It clears trailing spaces but keeps the indentation that gives the text its structure.

Note that in Markdown, two spaces at the end of a line can mean a line break, so End only removes those breaks too.

Trimming fits well before other list tools. After it, remove duplicate lines so that “anna” and “anna “ count as the same, or sort the lines so indented entries do not jump to the top.

How it works

The text is split at each line break. Each line is passed through the browser’s built-in trim function for the side you chose (trim, trimStart or trimEnd), and the lines are joined again in their original order. Nothing else in the line changes, and the text never leaves your device.

Worked examples

The first two examples use three lines: “ Name “ (spaces on both sides), a tab + “Age” + a tab, and “ City”.

Both ends. Every line is cleaned on both sides, giving Name, Age and City on three lines.

Start only. Leading spaces and tabs go, trailing ones stay. “Name” keeps its three trailing spaces and “Age” keeps its trailing tab, while “City” has nothing left to trim at the end.

End only, for code. A two-line Python snippet where the second line ends in three stray spaces. End only gives def f(): and return 1: the 4- and 8-space indents are untouched and the trailing spaces are gone.

Limits and tips

  • Spaces in the middle of a line are left alone. Pair this with Remove Extra Spaces for a full clean-up.
  • To convert indentation rather than remove it, use Tab to Spaces.

Frequently asked questions

Does trimming remove spaces between words?
No. Only the whitespace before the first visible character and after the last one is removed. "a b" stays "a b". To squeeze runs of spaces inside a line, use Remove Extra Spaces.
Which characters count as whitespace?
Spaces, tabs, no-break spaces and the other Unicode space characters, plus the byte order mark that sometimes sits at the start of a file. Zero-width spaces are not whitespace here; remove those with the Invisible Character Remover.
Will it delete empty lines?
No. A line that held only spaces becomes empty, but it stays in place, so the number of lines never changes. Run Remove Empty Lines afterwards if you want the gaps gone.
How do I clean code without losing indentation?
Choose End only. Trailing spaces go, and the indentation at the start of each line stays exactly as it was.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close