简体   繁体   中英

How to use vim autowrap correctly?

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?

You can run :help fo-table to see explanations of the options:

a   Automatic formatting of paragraphs.  Every time text is inserted or
    deleted the paragraph will be reformatted.  See |auto-format|.
    When the 'c' flag is present this only happens for recognized
    comments.

This means that every time you insert a character, vim will try and autoformat the paragraph. This will cause it to move everything back onto the same line.

I don't think you need to add a at all. I use neovim, but the behavior here should be the same. The default values are, according to the help pages:

(default: "tcqj", Vi default: "vt")

Try removing set fo+=a entirely from your .vimrc . Keep set textwidth=80 . That should fix your issue.

EDIT : Once you have set textwidth=80 , if you want to format an existing paragraph, you can highlight it in visual selection and press gq .

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

The following allows me to use the enter key to start a new line while setting the text width to be 79 characters:

set tw=79 "width of document                                                                            
set fo=cqt                                                                      
set wm=0 "# margin from right window border

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM