Caxton
Features Benchmarks Pricing Docs How it works Changelog Support About Download Free
Guides
All guides Large File Text Editors EmEditor for Mac UltraEdit Alternatives Notepad++ Alternatives Notepad for Mac Free Text Editors for Mac File Too Large for VS Code CSV Editor for Mac Open a CSV File CSV Viewer for Mac Open a TSV or Pipe-Delimited File Edit CSV Without Auto-Formatting Repair a Malformed CSV Remove Duplicate Rows Validate a CSV Transform a CSV Column Fixed-Width to CSV Change a CSV Delimiter Split a Large CSV Combine CSV Files Sort a Large CSV CSV Too Big for Excel Open a Huge CSV Log Viewer for Mac View Log Files Follow a Live Log File CMTrace for Mac Open a 10 GB Log Filter Large Log Files Jump to a Time in a Log Find & Replace in Huge Files Find & Replace Across Files Regex Cheat Sheet Compare Two Text Files Open a SQL Dump Fix Garbled Text & Encoding Convert a File to UTF-8 Remove a BOM Convert Line Endings Remove Invisible Characters Smart Quotes to Straight Quotes Sort & Dedupe Lines Open a GBK or Big5 File Open a UTF-16 or Shift-JIS File Open a Large JSON File Numbers File Too Large

How to find and remove invisible characters on a Mac.

A lookup returns nothing for a customer whose name is right there in the sheet. An import rejects a code that is spelled correctly, or a search for New York finds one row where the screen shows two. When two pieces of text look identical and still do not match, one of them usually holds a character you cannot see: a non-breaking space, a zero-width space, a soft hyphen, a stray byte order mark or a control character. This guide explains what those characters are, shows how to find them with tools that are already on your Mac, and shows how to remove them without harming the text around them.

Download Caxton for Free · 7-day free trial of Caxton Pro, no credit card required · 8 MB · macOS 13.0+

The short answer

To find invisible characters on a Mac, print the file's bytes with hexdump -C in Terminal, then remove them with a single perl command that deletes zero-width spaces, soft hyphens and stray byte order marks and turns non-breaking spaces into ordinary spaces, using tools that are already installed. Caxton, a native Mac editor for text and data files, marks each hidden character in the text with View ▸ Show Invisibles and removes them with Text ▸ Clean Up ▸ Remove Invisible Characters, after a preview that counts every kind it found. That command leaves in place the joiners and direction marks that emoji and right-to-left text need.

What are invisible characters, and where do they come from?

Every character in a text file is stored as a number, called its code point and written like U+0041 for the letter A. Most code points draw something. A few draw nothing, or draw a blank that looks exactly like a space, and those are the ones that cause trouble. Five kinds account for most of it.

  • The non-breaking space, U+00A0. It looks like a space and is a different character. The Unicode Standard gives it "the same width as U+0020 SPACE" and adds that "no line breaks are permitted between it and surrounding characters," which is how a page keeps two words on one line. Microsoft's page on the TRIM function describes it as "commonly used in Web pages."
  • The zero-width space, U+200B. The standard defines it as a character that "indicates a word break or line break opportunity, even though there is no intrinsic width associated with this character." It is intended for languages such as Thai and Japanese that are written without visible spaces between words, and it also travels inside text copied from web pages and messages, where nothing on screen gives it away.
  • The soft hyphen, U+00AD. In the standard's words it "is not a hyphen, but rather an invisible format character used to indicate optional intraword breaks." It marks where a long word may be split at the end of a line, and it stays inside the word when the text is copied somewhere else.
  • The byte order mark, U+FEFF. At the very start of a file it is a signature. The Unicode FAQ describes it as "the character code U+FEFF at the beginning of a data stream, where it can be used as a signature defining the byte order and encoding form." Anywhere else it is stray, and the same FAQ is plain about that: "when not at the beginning of a text stream, U+FEFF should normally not occur." It ends up in the middle when two files that each begin with one are joined.
  • Control characters. These are the codes below the space, U+0000 to U+001F, together with U+007F and the range U+0080 to U+009F. The tab and the line break are control characters that text needs. The others are left over from terminals, printers and old file formats, and a name or an amount should never hold one.

Nobody types these on purpose. They come along when text is pasted from Word, an email, a web page or a PDF, or when another system exports a file, and they stay when the text is saved as plain text or as a CSV. The file then looks right on screen, which is why the problem is usually found by a formula or an import and not by a person.

Why does text that looks identical not match?

A program does not compare how two values look. It compares the characters they are made of, one code point after another, and a non-breaking space is no more a space than the letter O is a zero. A small file makes this concrete. This command builds customers.csv with one hidden character in each of five rows, and every \x pair in it is one byte:

printf 'id,name,city\n1001,Hansen,New York\n1002,Dupont,New\xc2\xa0York\n1003,Rossi\xe2\x80\x8b,Boston\n1004,Novak,Spring\xc2\xadfield\n\xef\xbb\xbf1005,Meier,Austin\n1006,Okafor\x1f,Denver\n' > customers.csv

Written here without its hidden characters, the file holds this:

id,name,city
1001,Hansen,New York
1002,Dupont,New York
1003,Rossi,Boston
1004,Novak,Springfield
1005,Meier,Austin
1006,Okafor,Denver

Rows 1001 and 1002 both read New York. Ask grep to count the lines that contain it:

grep -c 'New York' customers.csv
1

The answer is 1, because row 1002 has a non-breaking space between the two words. A search for the name Rossi followed by its comma finds nothing at all, because a zero-width space sits between them:

grep -c 'Rossi,' customers.csv
0

The same thing happens wherever software compares text. A lookup formula returns no match, a database join drops the row, a filter leaves it out, and an import treats one customer as two or rejects a code it already has. Microsoft's guide to cleaning data in Excel describes the effect: such characters "can sometimes cause unexpected results when you sort, filter, or search," and "Because these characters are not easily noticed, the unexpected results may be difficult to understand."

Trimming does not always help. Microsoft documents that "The TRIM function was designed to trim the 7-bit ASCII space character (value 32) from text," and that "By itself, the TRIM function does not remove this nonbreaking space character." A value that still fails to match after a trim is a strong sign that the blank at its end is not an ordinary space. For the other things a spreadsheet does to a file as it opens it, see Edit a CSV without Excel changing the data. Curly quotes cause the same kind of mismatch, and they at least can be seen. How to convert smart quotes to straight quotes covers those.

How do I show invisible characters in a file on a Mac?

Terminal has everything this takes, already installed. The four commands below answer four different questions about the same file: what bytes it holds, where the odd ones sit in the text, which lines have them, and what each one is called. Visual Studio Code is a fifth way if you already edit there.

Print the bytes with hexdump -C or od -c

Apple's manual page describes hexdump -C as the "Canonical hex+ASCII display." Each row gives a position in the file, sixteen bytes in hexadecimal, and the same sixteen bytes as text between two bars, with a period standing in for every byte that is not a printable ASCII character.

hexdump -C customers.csv
00000000  69 64 2c 6e 61 6d 65 2c  63 69 74 79 0a 31 30 30  |id,name,city.100|
00000010  31 2c 48 61 6e 73 65 6e  2c 4e 65 77 20 59 6f 72  |1,Hansen,New Yor|
00000020  6b 0a 31 30 30 32 2c 44  75 70 6f 6e 74 2c 4e 65  |k.1002,Dupont,Ne|
00000030  77 c2 a0 59 6f 72 6b 0a  31 30 30 33 2c 52 6f 73  |w..York.1003,Ros|
00000040  73 69 e2 80 8b 2c 42 6f  73 74 6f 6e 0a 31 30 30  |si...,Boston.100|
00000050  34 2c 4e 6f 76 61 6b 2c  53 70 72 69 6e 67 c2 ad  |4,Novak,Spring..|
00000060  66 69 65 6c 64 0a ef bb  bf 31 30 30 35 2c 4d 65  |field....1005,Me|
00000070  69 65 72 2c 41 75 73 74  69 6e 0a 31 30 30 36 2c  |ier,Austin.1006,|
00000080  4f 6b 61 66 6f 72 1f 2c  44 65 6e 76 65 72 0a     |Okafor.,Denver.|
0000008f

Every hidden character is now a short run of bytes. In the fourth row, c2 a0 between 77 and 59 is the non-breaking space between the w of New and the Y of York. The row after it has e2 80 8b, the zero-width space after Rossi. Then come c2 ad for the soft hyphen inside Springfield, ef bb bf for the byte order mark in front of 1005, and a single 1f, the control character after Okafor.

od -c, which its manual page describes with "Output C-style escaped characters," prints the same bytes one to a column, as octal numbers where a byte is not printable. These are the fourth, fifth and sixth rows of its output:

LC_ALL=C od -c customers.csv
0000060    w 302 240   Y   o   r   k  \n   1   0   0   3   ,   R   o   s
0000100    s   i 342 200 213   ,   B   o   s   t   o   n  \n   1   0   0
0000120    4   ,   N   o   v   a   k   ,   S   p   r   i   n   g 302 255

LC_ALL=C in front makes od treat every byte on its own. Without it, od reads the two bytes of the non-breaking space as one character and prints the character itself, which is the thing you cannot see.

Make them visible in the text with cat -v

The manual page for cat describes its -v option in these words: "Display non-printing characters so they are visible." A control character is printed as a caret and a letter, and for the rest the page gives this rule: "Non-ASCII characters (with the high bit set) are printed as ‘M-’ (for meta) followed by the character for the low 7 bits."

LC_ALL=C cat -v customers.csv
id,name,city
1001,Hansen,New York
1002,Dupont,NewM-BM- York
1003,RossiM-bM-^@M-^K,Boston
1004,Novak,SpringM-BM--field
M-oM-;M-?1005,Meier,Austin
1006,Okafor^_,Denver

The text stays readable, and each hidden character shows up where it sits. LC_ALL=C matters here too. With Terminal's usual UTF-8 setting and no LC_ALL=C, cat -v passes the non-breaking space and the byte order mark through unchanged, so two of the five stay hidden. Put together, the two views give each character a signature you can recognize:

  • A non-breaking space, U+00A0, is the bytes c2 a0, and cat -v prints it as M-BM- followed by a space.
  • A zero-width space, U+200B, is the bytes e2 80 8b, and cat -v prints it as M-bM-^@M-^K.
  • A soft hyphen, U+00AD, is the bytes c2 ad, and cat -v prints it as M-BM--.
  • A byte order mark, U+FEFF, is the bytes ef bb bf, and cat -v prints it as M-oM-;M-?.
  • The control character U+001F is the single byte 1f, and cat -v prints it as ^_.

List the lines that hold them with grep

In a long file you first want to know which lines to look at. The pattern below matches any byte outside the range from the space to the tilde, which is the printable part of ASCII, and -n puts the line number in front of each line it finds. Piping the result through cat -v makes the finds visible:

LC_ALL=C grep -n '[^ -~]' customers.csv | LC_ALL=C cat -v
3:1002,Dupont,NewM-BM- York
4:1003,RossiM-bM-^@M-^K,Boston
5:1004,Novak,SpringM-BM--field
6:M-oM-;M-?1005,Meier,Austin
7:1006,Okafor^_,Denver

With -c in place of -n, grep prints only the number of such lines, which is 5 here and 0 for a clean file. Three things are worth knowing about this pattern. A tab is outside the range as well, so for a tab-delimited file write the pattern as $'[^\t -~]', which leaves tabs alone. Accented letters and emoji are not ASCII either, so their lines are listed too, and the next command tells them apart. And the grep -P form that many answers written for Linux give does not run here: the grep included with macOS stops with invalid option -- P.

Name each character with perl

Perl is included with macOS and can print the Unicode name of a character. This line prints the line number, the code point and the name of every character that is neither a tab nor printable ASCII:

perl -CSD -Mcharnames=:full -lne 'printf "%d: U+%04X %s\n", $., ord $1, charnames::viacode(ord $1) while /([^\t\x20-\x7E])/g' customers.csv
3: U+00A0 NO-BREAK SPACE
4: U+200B ZERO WIDTH SPACE
5: U+00AD SOFT HYPHEN
6: U+FEFF ZERO WIDTH NO-BREAK SPACE
7: U+001F INFORMATION SEPARATOR ONE

-CSD makes perl read and write UTF-8. Its manual page lists what the letters stand for: the S covers the standard streams, beginning with "STDIN is assumed to be in UTF-8," and the D covers files, beginning with "UTF-8 is the default PerlIO layer for input streams." ZERO WIDTH NO-BREAK SPACE is the formal name of U+FEFF, the byte order mark. An accented letter is listed as well, as U+00E9 LATIN SMALL LETTER E WITH ACUTE in the case of é, so one look tells a harmless letter from a hidden character.

Let Visual Studio Code highlight them

Visual Studio Code, which its FAQ describes as "free for private or commercial use," marks these characters while you edit. The release notes for version 1.63 introduced the feature: "All uncommon invisible characters in source code are now highlighted by default," and "characters that can be confused with ASCII characters are also highlighted." Three settings control it, editor.unicodeHighlight.invisibleCharacters, editor.unicodeHighlight.ambiguousCharacters and editor.unicodeHighlight.nonBasicASCII, and the notes add that they "can be set to false to disable the corresponding feature."

Three limits come from the same pages. "Markdown documents are not highlighted by default." Since version 1.65, "ambiguous and invisible Unicode characters are no longer highlighted if the surrounding characters visually indicate a non-ASCII script." And the notes do not list which invisible characters count as uncommon, so open a file you know, such as customers.csv, and check which of the five are highlighted before you rely on it. The highlighting shows the characters and leaves the removing to you.

How do I remove zero-width spaces and non-breaking spaces from a file?

Removing them is two different jobs. A non-breaking space stands where a space belongs, so it should become an ordinary space: deleting it would turn New York into NewYork. A zero-width space, a soft hyphen and a stray byte order mark take up no room, so they are deleted, and so is a control character that is not a tab or a line break.

One perl command for the common ones

perl -CSD -pe 's/[\x{200B}\x{2060}\x{00AD}\x{FEFF}]//g; s/[\x{00A0}\x{202F}]/ /g; s/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]//g' customers.csv > customers-clean.csv

The command has three parts. The first deletes the zero-width space, the word joiner U+2060, the soft hyphen and the byte order mark. The second turns the non-breaking space and the narrow non-breaking space U+202F into ordinary spaces. The third deletes the control characters below the space, except the tab, the line feed and the carriage return, and it deletes U+007F as well. The result goes to a new file and the original stays as it was. The two checks from earlier now give the right answers:

grep -c 'New York' customers-clean.csv
2
LC_ALL=C grep -c '[^ -~]' customers-clean.csv
0

To change the file itself, put -i.bak after -CSD and leave off the redirection. Perl's manual page explains that the extension "is used to modify the name of the old file to make a backup copy," so the untouched original stays beside the cleaned file as customers.csv.bak:

perl -CSD -i.bak -pe 's/[\x{200B}\x{2060}\x{00AD}\x{FEFF}]//g; s/[\x{00A0}\x{202F}]/ /g; s/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]//g' customers.csv

Two notes on this command. Its first part also removes a byte order mark at the very start of a file, where the mark is a signature and not a stray. If the program that reads the file expects that mark, take \x{FEFF} out of the first pattern. The command also expects UTF-8. If the file is in another encoding, convert the file to UTF-8 first, and fix garbled text and the wrong encoding shows how to tell which encoding a file is in.

The same job with sed and tr

The sed included with macOS is BSD sed, and its manual page documents no escape for a byte by its hexadecimal value. The shell fills the gap. Inside $'…', zsh and bash turn \xc2\xa0 into the two bytes of a non-breaking space before sed sees them. Each -e below is one substitution, for the non-breaking space, the zero-width space, the soft hyphen and the byte order mark, in that order:

sed -e $'s/\xc2\xa0/ /g' -e $'s/\xe2\x80\x8b//g' -e $'s/\xc2\xad//g' -e $'s/\xef\xbb\xbf//g' customers.csv > customers-sed.csv

That leaves the control character. tr -d, described in its manual page with "Delete characters in string1 from the input," removes it. The ranges are octal, and they skip the tab, the line feed and the carriage return:

LC_ALL=C tr -d '\000-\010\013\014\016-\037\177' < customers-sed.csv > customers-sed-clean.csv

The file this writes is byte for byte the one the perl command wrote. If sed stops with RE error: illegal byte sequence, the file holds bytes that are not valid UTF-8, which usually means it is in another encoding. With LC_ALL=C in front, sed compares plain bytes and runs anyway.

What a cleanup should leave alone

Not every invisible character is a mistake. The zero-width joiner U+200D builds emoji: the Unicode emoji standard explains that it "can be used between the elements of a sequence of characters to indicate that a single glyph should be presented if available." The joiner and the zero-width non-joiner U+200C also change how letters connect, an effect the Unicode Standard notes "is sometimes required in writing Persian using the Arabic script." Direction marks such as U+200E and U+200F keep right-to-left text in order, and the standard warns that "text processes that modify text content must maintain these characters correctly."

The perl and sed commands above name the characters they change, and they touch none of these. A broader pattern does. Deleting the whole Unicode format category in perl, with s/\p{Cf}//g, also deletes the joiner inside the woman technologist emoji and leaves two emoji, a woman and a laptop. A command that strips every non-ASCII byte goes further and takes every accented letter with it.

For a folder of files, or a cleanup that runs every night, these commands are the better tool. They need no window, and the same line works on every file.

How do I find and remove invisible characters in Caxton?

Caxton has a view that marks these characters, an Inspector that counts and names them, and a command that removes them after a preview. All three read one table of characters, so what is marked is what is counted and what is removed. Here is the walk with the same customers.csv.

1. Open the file and switch to the text view

Drag the file onto Caxton, or choose File ▸ Open… and pick it. A .csv opens as a table in the grid, and the two commands in the next steps belong to the text view, so choose View ▸ CSV Grid View or press ⇧⌘C to see the file as text. A file without table structure, such as a log or a document, opens as text already. Opening and viewing are Caxton Free at any file size.

2. Turn on Show Invisibles

View ▸ Show Invisibles draws faint marks over the text: a dot for every space, a small arrow for every tab and a ¬ at every line break. A hidden character gets a mark of its own, and the mark shows what the cleanup command would do with it:

  • A character the command removes, such as a zero-width space, a soft hyphen, a stray byte order mark or a control character, gets an orange tinted cell. The cell is drawn at least two points wide, so a character with no width still shows.
  • A non-breaking space or a narrow non-breaking space gets the dot of a space on an orange cell, which sets it apart from the plain dot of an ordinary space.
  • A character the command leaves in place, such as a direction mark or a variation selector, gets a muted bar. A joiner inside an emoji, or inside a word of a script that uses joiners, gets no mark.

In customers.csv five orange cells appear, one in each of the five rows that hold a hidden character. Show Invisibles marks where a character is and how the command would treat it. It does not write which character it is, and the next step does that. The setting is Caxton Free at any file size, and it stays on until you turn it off.

3. Count them and name one in the Inspector

View ▸ Show Inspector, or ⌥⌘I, opens a column at the right of the window. In its Encoding section the Invisibles row lists every kind in the file with its count. For customers.csv that is 1 zero-width space, 1 soft hyphen, 1 stray byte-order mark, 1 control character and 1 non-breaking space.

To learn what one character is, put the caret just in front of its orange cell. The Character row of the Selection section then gives its code point, its Unicode name and its bytes, for example U+200B, ZERO WIDTH SPACE, bytes E2 80 8B. The grid has a check of its own: select the cell that reads Rossi, and a Characters row gives 6 characters, 8 bytes for a five-letter name. The Inspector is Caxton Free at any file size.

4. Run Remove Invisible Characters and read the preview

Choose Text ▸ Clean Up ▸ Remove Invisible Characters. It works on the selection, or on the whole document when nothing is selected. Caxton scans first and then opens a preview attached to the window, and nothing has changed yet. For customers.csv the preview is headed Remove Invisible Characters in the whole document? and gives 5 changes, then one sentence for each kind it found:

1 zero-width space will be removed.
1 soft hyphen will be removed.
1 stray byte-order mark will be removed.
1 control character will be removed.
1 non-breaking space will become a space.

Below the counts are up to five samples from the text, each shown before and after, with every character the command changes written as its code point between angle brackets, such as ⟨U+00A0⟩. Without that, a removed zero-width space would look the same on both sides. Cancel closes the sheet and leaves the file alone, and the button named Remove Invisible Characters applies the change.

The preview of Remove Invisible Characters in Caxton gives 5 changes, one sentence for each kind of character it found, and five before and after samples in which each hidden character is written as its code point.

This is the preview for another file, from Caxton's manual: a count for each kind, then samples with each hidden character written as its code point.

Remove Invisible Characters is an edit to the text, so it follows the rule for editing. On Caxton Free it runs in full, preview included, in a file that was at most 10 MB when it was opened. In a larger file it is Caxton Pro: on Caxton Free the command is refused with a banner and no preview opens, though the Inspector still counts the characters. Every download starts with a 7-day free trial of Caxton Pro with no credit card required.

5. What the command removes, converts and leaves in place

The command knows twelve kinds of character. It removes five, turns two into ordinary spaces, and counts five more that it leaves where they are, each with its reason in the preview.

The characterIts code pointsWhat the command does
The zero-width spaceU+200BIt is removed.
The word joinerU+2060It is removed.
The soft hyphenU+00ADIt is removed.
A stray byte order markU+FEFF, anywhere in the textIt is removed.
Control charactersU+0000 to U+0008, U+000B, U+000C, U+000E to U+001F, U+007F to U+0084 and U+0086 to U+009FThey are removed.
The non-breaking spaceU+00A0It becomes an ordinary space.
The narrow non-breaking spaceU+202FIt becomes an ordinary space.
The zero-width joinerU+200DIt is counted and left in place, because emoji and some scripts need it.
The zero-width non-joinerU+200CIt is counted and left in place, because some scripts need it.
Direction marksU+061C, U+200E, U+200F, U+202A to U+202E and U+2066 to U+2069They are counted and left in place, because right-to-left text needs them.
Variation selectorsU+FE00 to U+FE0F and U+E0100 to U+E01EFThey are counted and left in place, because they choose how an emoji or a symbol is drawn.
Line separatorsU+0085, U+2028 and U+2029They are counted and left in place, because this command never changes line breaks.

Tabs, line feeds and carriage returns are never touched, and no line ending changes. Other space characters, such as the thin space U+2009 and the ideographic space U+3000, are not in the table, so the command neither changes nor counts them. A byte order mark at the very start of a file belongs to the file's encoding and is not part of the text, and Text ▸ Byte Order Mark ▸ Remove leaves that one out of the next save. How to remove a BOM from a file on a Mac covers the mark at the start of a file: how to see it, remove it and check that it is gone. When a document holds only characters from the last five rows, no sheet opens. A banner reports that there is nothing to remove and lists what was found and left in place.

6. Clean one column of a CSV with Transform Cells

The text command cleans a whole file or a selection of text. To clean one column and leave the rest of the table as it is, stay in the grid, click the column's header and choose CSV ▸ Transform Cells…, or press ⌥⇧⌘T. In the sheet's Transform menu the Cleanup family holds Straighten Quotes, Remove Invisible Characters and Repair Mojibake. Remove Invisible Characters reads the same table as the text command, so a cell and a document are cleaned by one rule.

The sheet's Before and After table shows sample values with each hidden character written as its code point, and Apply… opens a preview with the number of cells that change. A column in scope means every data row of that column and never the header row. Transform Cells changes the table, so its sheet and its preview open on Caxton Free, and applying it is Caxton Pro. Transform a CSV column walks through the sheet. On Caxton Free, the text command in step 4 cleans the same CSV as text when the file is within the limit.

7. Undo, save, and the limits

The cleanup lands as one undo step, so ⌘Z puts every byte back, and ⌘S writes the file. Three limits apply. The command changes up to 256 MB in one step, and for more than that it asks you to select a smaller part. It does not rewrite bytes that are not valid UTF-8: if the text holds one, the command is not run, and a banner names the first line that has such a byte and points to File ▸ Reopen with Encoding. And with a line filter on, the command changes the visible lines only, and the heading of the preview names that scope.

Which way of removing invisible characters fits which job?

The jobTools you already haveIn Caxton
You want to know whether a file has hidden characters at all.LC_ALL=C grep -c '[^ -~]' counts the lines that hold a byte outside printable ASCII.The Invisibles row of the Inspector counts them by kind, on Caxton Free.
You need to know exactly which character one of them is.hexdump -C shows its bytes, and the perl line gives its code point and name.The Character row of the Inspector gives its code point, its name and its bytes.
One file needs cleaning once.The perl command writes a clean copy and keeps the original.Text ▸ Clean Up ▸ Remove Invisible Characters shows the count of each kind first.
Only one column of a CSV should change.The commands here work on whole lines, so this takes a script that reads the CSV.CSV ▸ Transform Cells… cleans the column you choose, and applying it is Caxton Pro.
The text holds emoji, Arabic, Hebrew or Persian.The perl and sed commands here leave joiners and direction marks alone, and broader patterns do not.The command counts those characters and leaves them in place.
The same cleanup runs on many files or on a schedule.A script is the better tool here.Caxton cleans the document that is open in front of you.

Open the file, turn on View ▸ Show Invisibles, and read the count of each kind in the preview before anything is removed.

Download Caxton for Free

7-day free trial of Caxton Pro, every feature, no credit card required. After it, Caxton Free keeps opening, searching and filtering at any size, and you can edit and save files up to 10 MB as text.

Frequently asked questions

Why do two identical-looking values not match?

A program compares the characters stored in the file and not the way they look. One of the two values usually holds a character you cannot see, such as a non-breaking space where the other has an ordinary space, or a zero-width space after the last letter. A lookup, a join, a search and an import all treat the two as different text. Print the bytes with hexdump -C, or turn on View ▸ Show Invisibles in Caxton, and the value that carries the extra character stands out.

What is a zero-width space?

A zero-width space is the character U+200B. The Unicode Standard defines it as a word break or line break opportunity that has no width of its own, intended for languages that are written without visible spaces between words. It takes no room on screen, so text that holds one looks unchanged, and in UTF-8 it is stored as the three bytes E2 80 8B. It usually arrives inside text copied from a web page or a message.

What is a non-breaking space?

A non-breaking space is the character U+00A0. It has the same width as an ordinary space, and it tells a program not to break the line at that point. Because it is a different character from the ordinary space U+0020, a value that holds one does not match the same value typed with the space bar, and Microsoft documents that the TRIM function in Excel does not remove it by itself. In UTF-8 it is stored as the two bytes C2 A0.

Will removing invisible characters break emoji or Arabic and Hebrew text?

It can, when a cleanup deletes every invisible character it finds. The zero-width joiner U+200D holds the parts of many emoji together, the zero-width non-joiner U+200C changes how letters connect in languages such as Persian, and direction marks such as U+200E and U+200F keep right-to-left text in order. The perl and sed commands in this guide do not touch any of them. Remove Invisible Characters in Caxton counts them in its preview and leaves them in place, together with variation selectors and the Unicode line separators.

Sources