Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode inserting "non-breaking spaces" instead of regular spaces

Tags:

I am trying to figure out why VSCode sometimes decides to randomly insert "non-breaking spaces" U+00A0 instead of regular spaces, and how to stop this from happening?

This is a different type of space character which is causing issues, as seen in my previous question GitHub markdown not rendering and git detecting changes when no changes

This is happening to me on both Mac OS and Windows, and others have experienced this issue too. Although this question is about VSCode, this also happens on the online GitHub markdown editor.

How do I solve this problem?

Steps to reproduce

Edit a markdown .md file in VSCode for a while.

Put in lots of headings such as # my heading or ## my sub-heading.

If you have the markdown extension you can CTRL/CMD + SHIFT + P and Open preview to the side.

You should eventually notice that one of the headings doesn't render, and if you open the file in a hex editor such as HxD you'll notice that there is a non-breaking space instead of a regular space after the # character.

Possible causes

  • The backtick ` character on Windows can be combined with a letter on your keyboard for example a which results in à. Maybe the use of backticks is causing the space character to turn into a non-breaking space? However this wouldn't explain the problem on Mac OS, because backticks can't be combined with other characters.
like image 924
David Callanan Avatar asked Jul 23 '19 11:07

David Callanan


People also ask

How do you write the code for a non-breaking space?

Alternatively called a fixed space or hard space, NBSP (non-breaking space) is used in programming and word processing to create a space in a line that cannot be broken by word wrap. With HTML,   lets you create multiple spaces that are visible on a web page and not only in the source code.

Why is VS Code adding spaces?

What was the case with me is that I had the option Editor: Format on save mode to be set to file , this in combination with format on save would mean VS Code will try to format the entire file, resulting in spaces as defined in the formatter.


2 Answers

I have this a lot too on Mac OS, but it turns out I am still holding the option key slightly (or perhaps shift on your keyboard) when typing the space after just having used the modifier key to type #

I've installed https://marketplace.visualstudio.com/items?itemName=viktorzetterstrom.non-breaking-space-highlighter&ssr=false#overview to spot my mistakes early.

like image 65
Dries Avatar answered Oct 03 '22 01:10

Dries


I just spent quite a lot of time trying to figure out why a "mv foo bar" command I copy and pasted from VScode was returning command not found. Turned out the space in between mv and foo was a non-breaking space which I only determined by pasting it into a unicode text converter. I would really like to know why this is happening and if it's a setting I can disable. I am using Windows 10 so I don't think it's just a Mac issue.

like image 27
bhffs Avatar answered Oct 03 '22 03:10

bhffs