Text, der aus einer Webseite, einer Textverarbeitung oder einem PDF kopiert wurde, bringt häufig Zeichen mit, die wie Leerzeichen aussehen, aber keine sind: geschützte Leerzeichen, das im CJK-Satz übliche breite Leerzeichen, Leerzeichen und Verbinder ohne Breite und die Bytereihenfolge-Markierung am Anfang vieler exportierter Dateien. Sie stören Suche, Vergleich und Code, und sie sind unsichtbar, während man sie sucht. Jedes gefundene Zeichen wird entweder in ein gewöhnliches Leerzeichen umgewandelt oder, wenn es gar keine Breite hat, ersatzlos entfernt, und die gefundene Anzahl wird gemeldet.
Die übrigen Schritte laufen in fester Reihenfolge: zuerst die unsichtbaren Zeichen, dann Tabulatoren aufgelöst in eine gewählte Zahl von Leerzeichen zwischen eins und acht, dann Folgen von Leerzeichen und Tabulatoren auf ein Leerzeichen zusammengezogen, dann Leerraum am Anfang und Ende jeder Zeile beschnitten, dann Leerzeilen verworfen. Zusammenziehen und Beschneiden rühren Zeilenumbrüche nie an, die Gestalt des Dokuments ändert also nur die letzte Option.
Was Sie vor dem Ausführen wissen sollten
-
Die gemeldete Zahl ist eine Nettodifferenz:
Das Auflösen von Tabulatoren fügt Zeichen hinzu, ein Dokument voller Tabulatoren kann also länger enden, als es begann — und die Zahl zeigt genau das, statt es zu beschönigen.
-
Zeichen ohne Breite werden gelöscht, nicht ersetzt:
Sie hatten von vornherein keine Breite. Geschützte und andere ungewöhnliche Leerzeichen werden zu gewöhnlichen Leerzeichen, denn dort sollte etwas stehen.
-
Bei Code ist Vorsicht geboten:
Folgen zusammenzuziehen und Zeilen zu beschneiden ebnet die Einrückung ein. Lösen Sie bei Quelldateien Tabulatoren auf und säubern Sie unsichtbare Zeichen, und lassen Sie die anderen beiden Optionen aus.
-
Zeilenumbrüche überstehen alles außer der letzten Option:
Nur das Verwerfen von Leerzeilen ändert, wie viele Zeilen Sie am Ende haben. Alles andere arbeitet innerhalb der Zeilen, die schon da sind.
The character count can go up
That surprises people, and it is correct.
Expanding a tab into four spaces adds three characters. If your text has more tabs than it has runs of redundant spaces, cleaning it makes it longer. The tool reports the net difference honestly rather than assuming the number must fall.
The [msm_tool slug="whitespace-cleaner"] does several distinct jobs, and it is worth knowing which you actually want.
The operations, and when each matters
- Trailing whitespace — spaces at the end of a line. Invisible in almost every editor, and the single most common cause of a diff showing changes you cannot see. Removing it is nearly always safe and nearly always worth doing.
- Leading whitespace — indentation. Safe in prose. Destructive in Python, YAML and Markdown code blocks, where indentation is syntax.
- Multiple spaces to one — tidies the double space after a full stop and the accidental triple space mid-sentence.
- Blank lines — collapsing runs of empty lines to one. Useful on text pasted out of a PDF; destructive on Markdown, where a blank line separates paragraphs.
- Tabs to spaces — or the reverse. A preference in code, and it should match whatever the project already does.
Where whitespace is meaningful
Run the wrong operation on the wrong content and the result is broken rather than tidy:
- Python. Indentation defines block structure. Stripping it destroys the program.
- YAML. Same — nesting is expressed entirely through indentation, and a valid file with the wrong indentation means something different.
- Markdown. Four leading spaces marks a code block; two trailing spaces force a line break; a blank line separates paragraphs. All three are whitespace that carries meaning.
- Fixed-width data. Column-aligned text where position is the delimiter.
- Poetry and formatted quotations, where the line shape is the point.
Invisible characters that are not spaces
The awkward ones. Text copied from a website or a word processor frequently carries characters that look like spaces and are not:
- Non-breaking spaces — common in text pasted from HTML or Word. They look identical to a normal space and behave differently, and they break string comparisons and searches.
- Zero-width characters — invisible entirely, and a known source of mystery bugs in identifiers, passwords and API keys.
- Other Unicode spaces — en space, em space, thin space, all of which display as a gap and are not the space character.
If two strings look identical and refuse to match, one of these is usually why.
The usual reasons to clean
- Text pasted from a PDF, which arrives with line breaks at every visual line and inconsistent spacing throughout.
- Content moving between a word processor and a CMS, which carries non-breaking spaces and stray formatting.
- Preparing text for comparison, where invisible differences would otherwise dominate the result.
- Tidying a code file before committing, where trailing whitespace produces noise in every future diff.
Work on a copy
Cleaning is not reversible, and the operations that are destructive are destructive in ways that are not obvious until later — a YAML file that still parses but nests differently, a Markdown document whose code blocks became paragraphs. Keep the original until you have checked the result.