# 0x0082.io

Dealing with unintended non-breaking spaces

Non-breaking spaces might look like normal spaces, but will cause problems if used unintentionally in commands and code.

Luckily, there are several quite simple solutions, many of them listed in thisthread on Super User. For me this is mostly an issue in iTerm2, so I have created a key binding to override the key combination for non-breaking spaces, and insert a normal space instead. To do this, go to Preferences > Keys and press the + button in the bottom left corner. Then press option + space in the Keyboard Shortcut field, set Action to Send Text, and enter a single, normal space in the blank field that appears below.

To make existing non-breaking spaces stand out in Vim, I added the following snippet to my .vimrc. This will make them display as bullets () instead of spaces.

set list
set listchars=nbsp:•

I prefer these solutions rather than overriding the key combination system-wide, as there might be situations where I actually want to use a non-breaking space. By only limiting the fix to iTerm2, I can still easily use them in other applications when needed.

How this became a problem in the first place

On MacOS, the key combination for a non-breaking space is option + space. When inserting other characters with key combinations that include option—such as the pipe character (option + 7 on my Norwegian keyboard)—followed by a space, a non-breaking space might be incidentally inserted if you don’t immediately let go of the option key before pressing space. Take this example:

$ ls -la | grep sh

The non-breaking space after the pipe character is not visible here, but running this command will result in the following:

zsh: command not found:  grep

You might notice the extra space before grep and think but of course, that’s not supposed to be there, but I didn’t, and it took me some time to figure it out.

One could say that the real cause of this problem is that I don’t let go of one key before pressing another, and while that may be true, I find it much easier to eliminate the problem by technical means than to change the way I type. And the problem has indeed been eliminated: After implementing the fixes I have mentioned in this post, I have not incidentally inserted a non-breaking space even once!