KLARTEXT GUIDE

How to remove trailing whitespace on Mac

In KlarText, choose Edit ▸ Text Transforms ▸ Strip Trailing Whitespace. Spaces and tabs at the end of every line are removed. Turn on Settings ▸ Editor ▸ Strip Trailing Whitespace on Save to have it happen automatically.

Trailing whitespace is the classic invisible defect: spaces or tabs sitting at the end of a line where nothing shows them. It survives copy and paste, it makes a one-character change look like a whole-line change in a diff, it triggers linter warnings, and in Markdown two trailing spaces silently become a line break you did not ask for.

Because you cannot see it, you cannot fix it by hand with any confidence.

The Terminal way

sed -i '' -E 's/[[:space:]]+$//' file.txt

The empty '' after -i is the BSD sed quirk that catches everyone coming from Linux — leave it out on macOS and sed treats the next argument as a backup suffix and fails, or worse, writes a file you did not expect. And this edits in place with no undo, on a file you cannot see the effect on.

The KlarText way

Edit ▸ Text Transforms ▸ Strip Trailing Whitespace

Every line in the document loses any spaces and tabs at its end. Trailing blank lines at the end of the file are also collapsed, leaving a single final newline — the form most tools and version control systems expect.

With text selected, only that selection is cleaned, expanded to whole lines. With nothing selected, the whole document is. One ⌘Z undoes all of it.

Make it automatic

Rather than remembering to run it:

Settings ▸ Editor ▸ Strip Trailing Whitespace on Save

With that on, every save cleans the document first. It is off by default — KlarText does not modify your files behind your back unless you ask it to.

Actually seeing the whitespace

View ▸ Show Invisibles

Spaces are drawn as ·, tabs as , and line ends as . Trailing whitespace becomes visible as a run of middle dots before the pilcrow, so you can confirm the problem exists and confirm it is gone. It is the single most useful setting for this class of problem, and it costs nothing to leave on.

Trailing whitespace is also the reason a deduplication can quietly under-perform: two lines that look identical but differ by one trailing space are not duplicates. Strip whitespace first, then see how to remove duplicate lines on Mac.

Related