25

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.

2
  • You can change your keybindings as Michael suggests: superuser.com/a/142573
    – user12125
    Commented Apr 8, 2017 at 10:39
  • 1
    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
    Commented Apr 8, 2017 at 16:42

5 Answers 5

30

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.

5
  • 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 ! Commented Apr 7, 2017 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
    – mike3996
    Commented Apr 7, 2017 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 Commented Apr 7, 2017 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! Commented Apr 7, 2017 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
    Commented Apr 8, 2017 at 19:58
20

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.

4
  • 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 ! Commented Apr 6, 2017 at 20:40
  • It’s a neat idea, but setting a mark and returning to it has two problems: it has to pollute one of the registers, and it will return you the the proper line but not to the proper column. Commented Oct 12, 2020 at 18:10
  • 1
    @JúdaRonén If you use the backtick instead of the apostrophe for jumping, it gets you to the right column: vim.fandom.com/wiki/Using_marks
    – Endre Both
    Commented Oct 16, 2020 at 8:37
  • How didn’t I know that?! Thanks!! ^_^ Commented Oct 16, 2020 at 16:22
16

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:‗
2
  • 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 Commented Apr 6, 2017 at 20:29
  • 2
    @Sarumanatee If you add the line to your vimrc it will, in fact, highlight them as you type them.
    – Tumbler41
    Commented Apr 11, 2017 at 19:55
2

If you don’t want to type non-breaking spaces, you can just map them to normal spaces. Doing so globally can be done by this command (add it to your .vimrc or init.vim file):

execute "inoremap \u00A0 <Space>"

If you want to map only in the current buffer, use <buffer>, like so:

execute "inoremap <buffer> \u00A0 <Space>"

I added this to my ftplugin/tex.vim file, because I never want U+00A0 in TeX files (~ is used instead). You can do so in your ftplugin/css.vim.

If you do want to type a non-breaking space, you can easily do so in insert mode by hitting Ctrl+v and then typing a non-breaking space (:h i_CTRL-V for more information).

(Credit for the execute trick)

1
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.

1
  • 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
    Commented Apr 8, 2017 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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