Macos – Hidden characters inserted after pipe (|) followed by a space

keyboardkeyboard shortcutsmacosspecial characters

Very often, on my Mac, when I use the pipe (|) character followed by a space character, an invisible character will be inserted in between.

This is especially annoying when using the terminal, as it makes commands invalid.

If I type the following in iterm2, I often get the following:

ls | cat
zsh: command not found:  cat

If I hit the up-arrow-key to get my previous command, and then remove and reinsert the space between | and cat, the command will work.

When I copy paste the working and non working commands into a file, like this:

non-working: ls | cat
working: ls | cat

and open it in Hex Fiend it shows the following:

non-working: ls | cat
working: ls | cat

I've also experienced the same kind of issue in SublimeText2 using the square brackets ([]) followed by a space. So I don't believe its an issue with iTerm2.

Best Answer

What happens is that you keep pressing with the space bar, which inserts a non-breaking space, known in HTML through the   reference. This non-breaking space is useful in text processing where you want certain words to stick together, like the "OS" and "X" in "OS X".

Obviously, this is a unicode character (00A0), and therefore most of the command-line utilities will fail, such as bash seeing _cat (where _ is the non-breaking space), and Hex Fiend just using a bad representation for it. Some editors on the other hand will just silently strip it out (e.g. this editor right here), while others even have a special representation for it (e.g. TextMate showing a dot instead of the blank space).

There are two solutions: