24

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.
David Callanan
  • 5,601
  • 7
  • 63
  • 105

4 Answers4

24

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.

msouth
  • 832
  • 11
  • 21
Dries
  • 356
  • 4
  • 6
  • Thanks I'll try the extension – David Callanan Nov 15 '19 at 15:36
  • 2
    There's an extension called [Fix Irregular Whitespace](https://marketplace.visualstudio.com/items?itemName=karlito40.fix-irregular-whitespace) which replaces this particular character on save (as opposed to highlighting it). – Carl Sep 25 '20 at 13:41
  • When you say "option" did you mean "shift"? I normally hit shift-3 to type a #. This GitHub issue says that shift-space gives you a U+00A0: https://github.com/microsoft/vscode/issues/13239 – msouth May 06 '21 at 05:16
  • I did mean `option` but should have mentioned that I'm using a GB keyboard. – Dries May 20 '21 at 10:38
  • An extension isn't needed anymore. Simply activate View -> Appearance -> Highlight Whitespace, see https://stackoverflow.com/a/41398464/4654246 – LyteFM Feb 03 '23 at 08:33
  • @LyteFM I've tested this in a markdown file, and it doesn't highlight the non-breaking space as expected, it does seem to work in JSON files however, needs some more testing. – Dries May 09 '23 at 09:09
3

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.

bhffs
  • 123
  • 1
  • 6
  • I have also experienced the problem on Windows, so definitely not just a mac issue (although I haven't noticed it in a while). – David Callanan Apr 14 '20 at 22:16
  • I can confirm the same issue on Linux – BanAnanas May 06 '20 at 11:57
  • Nope, it seems to be Zero Width Space U+200b which doesn't get detected by extension mentioned by @Dries – BanAnanas May 06 '20 at 12:45
  • What a nightmare! I've suffered this from long time ago. Do you know who adds the non-break space? Is it VScode, maybe an extension? I've experienced this mainly with Markdown docs. – garciadeblas Jul 16 '20 at 09:26
2

I have faced this trouble several times when copy/pasting code from Microsoft Skype or Microsoft OneNote. Both programs convert regular spaces into special Unicode spaces when pasting into it.

By the way, since version 1.63, Visual Studio Code automatically highlights these special spaces.

Roland Sarrazin
  • 1,203
  • 11
  • 29
0

I ran into this too, but I'm sure I unwittingly inserted the non-breaking spaces using the spacebar alone. I found the hexdump plugin useful for diagnosing the problem. I still don't know how to avoid it.

glyn
  • 1,180
  • 8
  • 19