5

I'm using vim with pandoc and need to use non-breaking spaces fairly regularly, which is achieved in pandoc by backslash-escaping a regular space (i.e., "\ "). I would prefer to use hard line breaks in vim, but when I do so and a non-breaking space occurs at the end of a line, the space character is deleted and the backslash remains at the end of the line -- and this signals the end of the paragraph to pandoc.

Is there a way I can tell vim to treat "\ " as a printed character when it reformats paragraphs with hard line breaks?

As an example, if I write the following:

This line contains a non-breaking space\ because I don't want a break after space.

What I want vim to do is this:

This line contains a non-breaking
space\ because I don't want a break after
space.

But what it does is this:

This line contains a non-breaking space\
because I don't want a break after space.

My workaround for the moment is to stick to soft line breaks, and I've remapped h to gh etc. to make that easier to deal with. I'm also trying to figure out whether there is an alternative way to specify a non-breaking space to pandoc that will be less confusing to vim.

alanhr
  • 95
  • 1
  • 6
  • I think you can use ` ` as well, or just insert the proper UTF-8 char. – mb21 Jul 22 '15 at 14:06
  • Thanks @mb21, that's actually the direction I've gone for the moment. I'm using the UTF-8 character and pandoc seems happy with that, but I'm still not fully happy because I like having a visual indication in the buffer that the space is non-breaking. That seems like an easier problem to solve, though as a vim newbie it's taking some effort to get right. – alanhr Jul 22 '15 at 15:08

1 Answers1

8

Use the proper UTF-8 character for a non-breaking space (U+00A0) instead of \. In Vim, you can insert it with Ctrl-v x a 0.

In your ~/.vimrc file add the following to highlight non-breaking spaces:

set list
set listchars=nbsp:.
Community
  • 1
  • 1
mb21
  • 34,845
  • 8
  • 116
  • 142
  • 2
    Thanks again @mb21, that's fantastic! I use the non-breaking space quite a lot, so in the end I mapped it to ``. Since I'm often using it near decimal points, I adjusted your `listchars` suggestion to use a middle dot and colored it red with `highlight SpecialKey`. While I imagine I can tweak vim forever, this at least gets me to a setup I can get some real work done with while I continue learning. – alanhr Jul 23 '15 at 13:16
  • From [here](https://coderwall.com/p/07mtla/insert-non-breaking-space-in-vim), `ctrl-k`+`space`+`space` also inserts a non-breaking space (U+00A0). Same for `ctrl-k`+`N`+`S`. – Matthias Braun Jul 21 '23 at 13:25