简体   繁体   English

如何正确使用vim自动包装?

[英]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. 我正在尝试使用vim自动换行功能在输入时自动将段落自动换行成不超过80个字母的实时行。 This can be done by set textwidth=80 and set fo+=a . 可以通过set textwidth=80set fo+=a The a option of the vim formatoptions or fo basically tells vim to wrap the entire paragraph while typing. vim formatoptionsfoa选项基本上告诉vim在键入时包装整个段落。

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. 通常,我可以将光标移至“ a”并进入插入模式,然后按Enter。 But after set fo+=a , nothing will happen when I press enter in the insert mode at "a". 但是在set fo+=a ,当我在插入模式下按Enter在“ a”时什么也不会发生。 One thing I do notice is that if there is no space between "is" and "a", pressing enter will insert a space. 我注意到的一件事是,如果“ is”和“ a”之间没有空格,请按Enter键将插入一个空格。 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: 您可以运行:help fo-table来查看选项的说明:

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. 这意味着,每次插入字符时,vim都会尝试自动格式化段落。 This will cause it to move everything back onto the same line. 这将导致它将所有内容移回同一行。

I don't think you need to add a at all. 我认为您根本不需要添加a I use neovim, but the behavior here should be the same. 我使用neovim,但此处的行为应相同。 The default values are, according to the help pages: 根据帮助页面,默认值为:

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

Try removing set fo+=a entirely from your .vimrc . 尝试从.vimrc完全删除set fo+=a Keep set textwidth=80 . 保持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 . 编辑set textwidth=80 ,如果要格式化现有段落,可以在视觉选择中突出显示它,然后按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. 基本思想是,在进入换行符时,在发送<CR>时暂时禁用自动包装,然后再恢复自动包装。 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> 我现在使用的是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. 我之所以认为这不是最佳选择的原因是,由于某种原因,我无法以这种方式绑定<Enter> ,而不得不使用另一个键。

If <Enter> or <CR> can be configured in this way then the problem is 100% solved. 如果可以通过这种方式配置<Enter><CR> ,则问题将100%解决。

The following allows me to use the enter key to start a new line while setting the text width to be 79 characters: 以下内容使我可以使用Enter键在将文本宽度设置为79个字符的同时开始新的一行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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