2

I am trying to use the vim autowrap functionality to automatically wrap my paragraph into lines no longer than 80 letters in real time as I type. This can be done by set textwidth=80 and set fo+=a. The a option of the vim formatoptions or fo basically tells vim to wrap the entire paragraph while typing.

However, there is a very annoying side-effect, that I can no longer break a line by simply pressing enter.

This is a sample sentence.

Say for the above sentence, if I want to make it into:

This is

a sample sentence.

Usually I can just move the cursor to "a" and enter insert mode and then press enter. But after set fo+=a, nothing will happen when I press enter in the insert mode at "a". One thing I do notice is that if there is no space between "is" and "a", pressing enter will insert a space. But nothing else will happen after that.

So what do I miss here? How do I stop this annoying behavior?

3 Answers 3

1

Auto-formatting sounds like a nice feature, but in practice I've never found a use except for pure plain paragraphs of text.

You've correctly identified the conflict between Vim's automatic wrapping and the need to manually influence line breaks. Vim's :help autoformat offers the following workaround:

  • You need to properly define paragraphs. The simplest is paragraphs that are separated by a blank line. When there is no separating blank line, consider using the 'w' flag and adding a space at the end of each line in the paragraphs except the last one.

So, with :setlocal fo+=w, you can just press <Enter> to introduce a hard break. The downside is that all lines inside paragraphs have a trailing space character.

1
  • hey thanks for the answer. I just found a way to by pass this issue with key-binds. If you are interested please find the details in the answer I posted below.
    – hzh
    Feb 16, 2018 at 4:27
0

After some exploration, I find a workaround that can solve the problem to some extent, though not perfect.

The basic idea is that when entering a line break, disable the auto-wrapping temporarily when sending <CR> and resume auto-wrapping after that. There are multiple ways of doing that. And the best one as far as I know is using the paste mode, since you don't have to exit insert mode when entering paste mode. So just make the following commands into any key binding you like in insert mode. The one I am using right now is inoremap <C-N> <F2><CR><F2>

The reason why I think this one is not optimal is that for some reason I cannot bind <Enter> in this way, but have to use another key.

If <Enter> or <CR> can be configured in this way then the problem is 100% solved.

0

Another (suboptimal but maybe better) way to insert a linebreak is to position the cursor on an empty space between words, press r to enter replace (insert) mode, then press <Enter>.

r<Enter>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .