Anki inserts erroneous non-breaking spaces when pasting from clipboard

This is definitely not a critical issue, but it has been bugging me for a long time. All of my decks are literally infested with non-breaking spaces, which are not visible in the note editor, but can cause issues when displaying the card. Search for   in the card browser to see if you are also affected.

I don’t quite understand exactly when Anki decides to convert normal spaces into non-breaking ones, but it seems to be related to pasting from the clipboard—I do this a lot! Here are two different scenarios in which I could consistently reproduce this behavior in the note editor. You can open the HTML editor using Ctrl+Shift+X to see if spaces have been converted.

Pasting text from outside of Anki

Copy a text string from a different application to your clipboard. Type a space, then paste a text string using Ctrl+V (with formatting) or Ctrl+Shift+V (without formatting). The preceding space will have been converted.

Actually it seems Anki will immediately convert every typed space at the end of a text field to a non-breaking space and it will convert it back to a normal space if you continue typing after the space or you paste text which was copied from another text field inside of Anki.

Duplicating italic text containing a space

Type two words separated by a space and set one of them to italic. Copy or cut the text and paste it again into the same text field. The interword space will have been converted.

FWIW I am using Anki 2.1.42 on Linux, but this issue has been present at least since version 2.1.35.

3 Likes

Are you sure this is a problem caused by Anki? I always thought it would be the sources or the way my system handles the clipboard content.

I usually fix this with Find & Replace. Select all notes and do this:

Since ;nbsp is of no use in Anki cards (at least to me), this can’t cause any harm.

3 Likes

It might very well be caused by an interplay of Anki and the OS. But the first scenario I described above makes me believe this is at least partially Anki’s fault. I mean, the string from the clipboard must not even contain a space.

Find & Replace is definitely a convenient solution I haven’t thought of for some reason …

Looks similar to Unexpected non breaking space inserted after using mathjax or latex shortcuts in editor · Issue #1053 · ankitects/anki · GitHub

1 Like

I have this same problem.

I have a list of items stored as HTML so items have varying text color codes. I know there is not a single & n b s p ; (I inserted spaces; other just a space appears in the forum post!) in the original code anywhere, but when I copy from that file (as displayed in a browser, not the native HTML) and then paste into a regular Ankiweb field (I think both the “add” screen and if I’m editing something in the “browse” screen, spaces often (but not always?!?) become & n b s p ;

Furthermore, if I type something like " * " or " / " directly into a field after having pasted one of my items, more often than not what appears in Anki’s HTML is " * & n b s p ;" or " / & n b s p ;" And then if I copy the " * " from the regular Ankiweb field and paste it, it appears in Ankiweb’s HTML as “& n b s p ; * & n b s p ;”

I just noticed, though, that if I open a new “add” note and type " * " into a completely blank field and then look at Anki’s HTML interpretation, it shows as “& n b s p ; * & n b s p ;”

I really don’t understand this behavior. There’s been discussion about where this behavior comes from, but when it shows up just typing into a new field, I can’t help but think the problem lies with Anki, not the OS, but I really don’t know what I’m talking about.

I’ve never used search and replace, so thank you kleinerpirat for that tip to clean up my notes. But going forward . . . what to do?

I’m afraid the only option I have to ensure I’m getting what I want is to copy my original HTML code and paste into directly into Ankiweb’s HTML editor, field-by-field. If I need to type something, I need to type it into the HTML editor to be sure I know what I’m getting. Otherwise, I’ll end up with non-breaking spaces that really mess with the way the cards are displayed on my phone. It will be just a bit messy and not as easy as working with the WYSIWYG regular Anki fields.

If anyone has any other ideas, I’d love to streamline the process of creating and editing cards without having to worry about & n b s p ; !!

[Oh, just FYI: I’m using Version ⁨2.1.44 (b2b3275f)⁩
Python 3.8.6 Qt 5.14.2 PyQt 5.14.2
Windows 10 Home Single Language
Version 21H1
OS build 19043.1110]

This is the bane of my anki existence. Every time i type space and then paste something, it keeps the  

I followed the bug but i dont understand what is discussed there, just that is very hard to fix

Is it maybe possible via addon to remove any &nbsp in the field every time you paste text?

@kleinerpirat has made an awesome addon to deal with this

Thanks again!

3 Likes