Cleaning Text

How to remove trailing whitespace on Mac

You can do this with Note My in one undoable pass. Here is how to see the whitespace at the ends of your lines, take it off, and how your blank-line indentation and Markdown line breaks are protected while you do.

Half the file is marked as changed and the lines look identical — except one of them ends in a space, or a tab, or three of each. Trailing whitespace is invisible by definition, it survives every copy and paste, and it is the commonest reason a file looks modified when nothing meaningful happened to it. This guide covers how to see it in Note My, how to remove it from a whole file or just a selection, and how your text is protected in the two places where a trailing space is doing a job.

Note

Showing whitespace and removing it are two separate jobs. Showing it changes nothing in your file — you can leave it on permanently. Removing it edits the document, and a single undo takes it back.

Why trailing whitespace matters

It is not aesthetics. A run of spaces before a line ending is real bytes in the file, so every tool that compares text notices. Version control marks the line as modified. A linter fails a build over it. A here-document stops terminating because the closing delimiter has a space after it, and a continuation backslash stops continuing because it is no longer the last character on the line. It is also easy to create by accident: auto-indent leaves whitespace behind when you press Return on an indented line and move away without typing, and pasting from a rendered page brings padding with it.

Make it visible before you touch it

Before you strip anything, look at it. Two independent features can draw it.

The invisibles display. Open Settings with ⌘,, choose the Invisibles pane, and turn on Highlight trailing whitespace. That draws a translucent wash over the run of spaces or tabs before each line ending, so it becomes a visible block of colour. View ▸ Show All Characters switches it on together with the whitespace markers and the line-ending labels. Turning on Show whitespace in the same pane adds a middle dot · for each space and an arrow → for each tab, so you can see what the run is made of.

The text-cleanup markers. Separately, when you run a scan, our hidden-character scanner marks what it found in the text itself — trailing whitespace gets a bar, distinct from the markers used for other categories. That overlay is controlled by Highlight findings in Settings ▸ Text Cleanup, on by default. The markers clear as soon as you start editing, so finish your typing first and scan again when you want a fresh picture of what is left.

Neither writes anything to your file. What you see is drawn on screen only: it reaches neither an export nor the saved document. It all happens on your Mac — nothing is uploaded, and your files never leave the machine.

Where trailing whitespace sits in the category list

Our cleanup rules cover 21 categories of stray character, and Trailing whitespace is one of them. It is tightly drawn — a run of spaces or tabs immediately before a line ending, on content lines only — and its default action is remove. The categories are grouped into four presets, and the preset you pick decides whether trailing whitespace is cleaned at all.

Which text-cleanup presets include the trailing whitespace category
PresetCategoriesIncludes trailing whitespace?
Safe7No
Code-safe (default)16Yes
Prose6Yes
AggressiveAll 21Yes

Code-safe is the default and the right choice for most files. Safe is the only preset that leaves trailing whitespace alone, which is worth knowing if you have changed your default preset and then wondered why nothing was found.

Remove it from a file

  1. Open the review sheet

    From the toolbar's Clean menu, choose Clean Invisible / Special Characters…. The sheet opens over the document. Two routes lead to it, whichever is closer to hand: the Clean menu, or Review All… in the status-bar pill's popover.

  2. Set the scope

    The header carries a segmented control reading Selection and Whole Document. Choose Whole Document for the file, or select a block in the editor first and choose Selection to confine the pass to it. Line numbers are the real line numbers in the file either way, so a finding in Selection scope still points at the line you would go to in the editor.

  3. Choose the preset, or narrow it to one category

    Leave the preset pop-up on Code-safe to clean trailing whitespace along with the other fifteen categories in that set. To remove only trailing whitespace, tick that one category in the left-hand table and clear the rest — touching any checkbox switches the pop-up to Custom automatically.

  4. Scan

    Press Scan. The left table lists all 21 categories with a severity chip and a found count, so you can read the count next to Trailing whitespace before deciding anything. The right table lists the individual findings — Line:Col · Code Point · Category · Action · Risk. Double-click a finding to reveal it in the editor.

    Long files are not a problem here. Our large-file engine keeps them quick to work in, and the counts cover the whole file, so a log or a generated table you would never scroll through by hand is reported as fully as a short one. If the count is larger than you expected, read it before you act: it is a useful signal that a paste or an export brought padding in with it.

  5. Apply

    Press Apply — the button carries the count of what it will act on. It changes every finding whose category is ticked and whose own action is destructive, and the findings list refreshes so you can see what is left. The whole pass is a single undo step named "Clean Invisible Characters", so ⌘Z reverses all of it in one press. Apply comes alive only when the findings hold something to change, so every press you can make does real work on the document.

Warning

The review sheet acts on exactly the categories you tick, wherever they sit in the file. With the Aggressive preset that reaches smart quotes inside string literals and dashes inside comments as well. For a trailing-whitespace pass, stay on Code-safe or narrow the ticks to that one category, and the rest of your text is left as you wrote it.

Two places your text is protected on purpose

Blank lines keep their indentation

The category covers a run of space or tab before a line ending on content lines, so the pass concerns itself with what sits at the end of real content. A line made of nothing but spaces keeps them. That is deliberate: an indented blank line inside a function body is usually auto-indent holding your place, and emptying it would drop your cursor back to column one the next time you type there. When you do want those lines flattened as well, a rule of your own sweeps them in one pass, and the next section shows you how to write it.

Markdown hard line breaks survive the clean

In a Markdown file, your trailing spaces stay exactly where you put them. Our advanced language identifier reads the file you have open and fits the clean to it, so you can run the same pass over a README and a Swift file without stopping to think. Two spaces at the end of a Markdown line are a hard line break, and keeping them means your rendered document reflows the way you wrote it.

Other file types get the same care: mixed indentation is left alone in a Makefile, where a leading tab is load-bearing, and raised to the highest severity in Python.

If you do want them cleaned in a Markdown file anyway, you stay in charge: the removal rule described next is a separate pass that runs on whatever file type you point it at.

Make it a rule you can run on demand

The review sheet is a scan-then-decide workflow. To run the same clean repeatedly without reading a table each time, use the removal rules instead: a list of regular-expression rules you write and keep.

Choose Clean ▸ Edit Removal Rules… from the toolbar menu. A window titled Residual Removal Rules opens with 11 built-in rules — NBSP→Space, zero-width characters, soft hyphen and their kind. The built-ins stay as they shipped, which is what keeps them dependable: switch one off when you would rather it sat out, or duplicate it as the starting point for something of your own. For trailing whitespace, press Add and write the rule you want: give it a name, a pattern matching a run of spaces or tabs anchored to the end of a line, leave the replacement empty so the match is deleted, and turn on the option that makes anchors match at line boundaries rather than only at the ends of the document.

The Analyze panel is worth using before you trust the rule. Point it at the document or the selection and it reports per-rule match counts, regex errors, the enabled-rule count, the estimated replacements and the elapsed milliseconds. An invalid pattern turns its cell red with the error as a tooltip and is skipped rather than half-applied.

Once the rule is in place, Clean ▸ Clean Now runs it over the whole document as a single undo step named "Clean Residuals" — one command, one press, no table to read on the way through. It applies every enabled rule in the list, so read the Analyze counts first and switch off anything you are not ready to run. Rules import and export as JSON, so a set you have tuned is yours to keep and reuse, and Reset to Defaults brings the built-ins back whenever you want a clean slate.

Troubleshooting

Scanning for trailing whitespace from the status-bar pill

The pill keeps watch for the characters that are wrong in any file — zero-width characters, bidi controls, control characters and their kind — and its one-click Remove Invisible Character(s) action clears those where they stand. Trailing whitespace is a judgement call rather than a fault: a space at the end of a line is a legal character, and whether you want it gone depends on the file in front of you, so the decision stays yours. Open Review All… from the pill's popover, or reach for the Clean menu, and scan for it there with the full findings table in view.

I clicked Show Invisibles in the toolbar and the ends of lines look the same

The toolbar toggle and the View menu light up different sets, so you can pick the level of detail you want. The toolbar's Show Invisibles button brings up the whitespace markers, the line-ending labels and the underline. To add the wash over the trailing run, choose View ▸ Show All Characters, which brings the whitespace markers, the line-ending labels and the wash up together, or turn on Highlight trailing whitespace directly in Settings ▸ Invisibles and leave it on for good.

The scan found nothing in a file I know has trailing spaces

Three things to check, in order. Is the file Markdown? Your hard line breaks are protected there, and a removal rule strips them the moment you decide you want that. Is the preset Safe? Code-safe is the set that carries the category, so switch to it and scan again. Are the runs on lines that hold nothing else? Those keep their indentation, and a rule of your own flattens them the moment you ask it to. If none of the three fits, turn on Highlight trailing whitespace and look — what reads as a space is sometimes a non-breaking space, and the marker styles below name it outright.

I want to see exactly what the whitespace is made of

In Settings ▸ Invisibles, change the marker style. Symbols is the default. Code Points shows an escape for each character, independent of the file's encoding. Bytes shows the hex bytes that our encoding handling will write when you save — a space is 20 in UTF-8 and 20 00 in UTF-16 LE, and a non-breaking space is C2 A0 in UTF-8. That last mode is how you tell an ordinary space from a non-breaking one without guessing.

I applied the clean and want it back

Press ⌘Z. Both paths group their whole pass into one undo operation — "Clean Invisible Characters" for the review sheet, "Clean Residuals" for the rule sweep — so a single undo returns the document exactly as it was.

What else you can do from here

  • Nothing changes until you ask. Removing trailing whitespace is always a deliberate act — the Clean menu, the review sheet, or a rule you wrote and ran. The document sits exactly as you left it until you press Apply or Clean Now, and ⌘Z puts the whole pass back in one press if you change your mind.
  • Work through several files in one sitting. Open them as tabs and Find & Replace's Replace in All Files runs across every open tab at once, so a pattern you have proved on one file carries through the rest. Folder search reads and reports without writing a byte, which makes it the calm way to find out which files carry the padding before you open any of them.
  • Leave the view switched on for good. The trailing-whitespace wash and the marker styles are yours to leave on for good: they draw on screen only, never in the file and never in an export, so you can work with them on all day and watch a stray run appear the moment it lands.
  • Narrow the pass to a single category. Tick Trailing whitespace alone and the sheet becomes a one-purpose tool, useful when a file is going into review and you want the diff to show your work and nothing else.
  • Nothing is sent anywhere. Every scan and every clean happens on your Mac. Your files never leave the machine, and there is no account.

Frequently asked questions

Does removing trailing whitespace change my line endings?

Your line endings come through exactly as they were. The trailing-whitespace category matches the run of spaces or tabs before the line ending and removes only that. Line endings are their own category with their own commands under File ▸ Line Endings, so you convert them when you choose to and never by accident.

Can I clean just the lines I have selected?

Yes. Select the block in the editor, open Clean ▸ Clean Invisible / Special Characters…, and set the scope control to Selection. If nothing is selected, the sheet tells you so rather than silently cleaning the whole file.

Why are the two trailing spaces in my Markdown file kept?

Because in Markdown two trailing spaces are a hard line break. Note My keeps them in place for exactly that reason, so your rendered document breaks where you meant it to. A custom removal rule will still strip them whenever that is what you want.

Is there a way to check a file without changing it?

Yes — that is what Scan does. The review sheet scans and reports, and nothing is modified until you press Apply. The Analyze panel in the removal-rules window does the same for regex rules, reporting match counts without running them.

Does it strip whitespace inside strings and comments too?

Yes. It comes off wherever it sits, comments and multi-line strings included. If a multi-line string relies on padding at the ends of its lines, clean in Selection scope around it. The categories needing real care are the context-dependent ones — smart quotes and dashes — which the review sheet rewrites everywhere too. That is why Aggressive is the wrong preset for a routine clean.