20

I am conscious this is a strange question, and this might not even be the right forum to post it. But this has been a problem for several years.

From time to time, let's say once a month, when I program, i introduce a No-Break space instead of a regular space, before opening a curly brace.

This happens let's say when I'm writing () {

This leads to mysterious lint bugs. CSS clauses that don't apply. Until I remember this might be it.

As far as I'm aware, this only happens in vim, but since it's my main editor it might just never have happened in other software. But none of my coworkers' in my last jobs ever had that problem, even though they use the same keyboard layout as me. I'm the only one to use vim regularly though, that's why I though of posting here...

This happened to me at least in ubuntu 14.04, 16.04, and mac os x.

:set list doesn't reveal it.

I'm using a french-canadien keyboard layout, meaning the brace character is done by pressing alt + `

I'm very tired of this causing problems, it's infrequent enough I keep forgetting about it.

If you cannot tell me why this keeps happening, some sort of config to highlight that character in some sort of obvious way would be a suitable answer and really help with this problem.

  • You can change your keybindings as Michael suggests: superuser.com/a/142573 – user12125 Apr 8 '17 at 10:39
  • Crap like this is why I finally switched my layout permanently from Swedish to American. I probably spend more time typing English + C + Perl + Bash + Vim than I do typing our funny å, ä and ö letters. Those are easily produced using a compose key. – pipe Apr 8 '17 at 16:42
24

Others have already provided work-arounds, but as for the reason why it happens, I bet you're accidentally pressing Alt+Space.

I'm using Xubuntu with the 105-key Finnish keyboard layout myself, so for me, typing { means pressing AltGr+7. I just did a quick test in xfce4-terminal (on the bash command line), and pressing AltGr+Space indeed produces a no-break space (U+00A0).

I bet what's happening is that you're pressing down the Alt key in anticipation of typing the brace, and sometimes that happens before you actually press the space bar, producing a non-breaking space. And since it looks identical to a normal space, you don't realize it has happened before something mysteriously breaks, making it hard for you to notice it happening and thus also hard to unlearn that habit.

  • Oooh I'm so going to try this tomorrow at work. I always put a space before my curly brace, It'd make sense I'd have my finger on the shift key too early from time to time ! – Sarumanatee Apr 7 '17 at 0:15
  • 2
    @Sarumanatee: on linux you can indeed disable nbsp character altogether. Perhaps Ubuntu's keyboard manager even has an option for it. If not, you can tune the keyboard map yourself (it's a matter of deleting one line, in my system the keymap would be /usr/share/X11/xkb/symbols/us) after finetuning call setxkbmap us or whatever language of your preference – unperson325680 Apr 7 '17 at 6:33
  • @progo YES. I'm on macos right now, and use Karabiner to fine-tune the keyboard layout since my keyboard's most bottom-left is "fn" by default, which I believe is completely dumb. You inspired me to check in there, and there is an option to change all non-breaking spaces to normal space – Sarumanatee Apr 7 '17 at 13:08
  • And that option confirms @Ilmari Karonen 's suggestion, as that karabiner option displays the commands to create nbsp: option+space, and option+shift+space. Though it turns out, it truly wasn't vim-related at all in the end! – Sarumanatee Apr 7 '17 at 13:10
  • Ilmari Karonen is right, so this is not about the explanation but just to describe my workaround. I have added a syntax match linking the non-breaking space to the Error highlighting group, so that it is visibly different from ordinary space. The same goes for soft hyphen and non-breaking hyphen (which are linked to two different highlight groups since I sometimes use all of these intentionally in text as opposed to code). – bpj Apr 8 '17 at 19:58
19

You can add an autocommand that replaces non-breaking spaces with regular ones upon saving particular file types, eg. by putting something like this in your vimrc:

augroup RemoveSpaces
  autocmd!
  autocmd BufWritePre *.css silent! :%s/\%u00A0/ /g
augroup end

You can tweak it by adding more file types or setting a mark and returning to it after the replace operation as otherwise you will be propelled to the start of the last line where a replacement has been made.

  • Ohh that's pretty clever. I don't mind being propelled places since that really doesn't happen that often, and it's much less annoying than spending time debugging things ! – Sarumanatee Apr 6 '17 at 20:40
13

The 'listchars' option provides a means to make 'list' display non-breaking spaces. It's not set by default, so you probably want to add it.

set listchars+=nbsp:‗
  • That helps indeed. I think I'll try to find out how to highlight the nbsp char all the time though, to try and catch it as I'm typing it :o – Sarumanatee Apr 6 '17 at 20:29
  • 2
    @Sarumanatee If you add the line to your vimrc it will, in fact, highlight them as you type them. – Tumbler41 Apr 11 '17 at 19:55
1

yes, that's probably alt+space, happens to me a lot when i use the pipe operator:

cat xyz.txt | grep abc

if you hold the alt-key too long, it will create a non breaking space after the pipe and bash will try to find a command " grep" and then telling you that grep was not found ..

on macos there was some trick to change the keymap, so every time you insert a non-breaking space, it will get converted/replaced by a regular. as far as i remember, the keymap is changed for the whole keyboard, so it fixes your problem in all applications.

  • alt+space has nothing to do with Vim. By default (as in: no user mappings), on most systems alt+space will generate some escape which will contain an Esc, which will exit the insert mode in Vim. And that's a bout it. The non-breakable space is a result of some input bus or input manager on your system, Vim/Gvim never sees it. – grochmal Apr 8 '17 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.