为什么我不能停止vim自动换行我的代码?

169

我无法停止vim自动换行我的Python代码。如果我输入:set nowrap,它仍会自动换行。

我可以按J键来合并代码的分裂行,看起来像是真正插入了回车符。我只是不明白为什么会这样以及如何停止它。


21
:set nowrap 只能防止行的显示换行,而不能阻止插入换行符。 - rampion
对于多个窗口,请运行::windo set nowrap - kenorb
9个回答

138
'textwidth' 'tw'        number  (default 0)
                        local to buffer
                        {not in Vi}
        Maximum width of text that is being inserted.  A longer line will be
        broken after white space to get this width.  A zero value disables
        this.  'textwidth' is set to 0 when the 'paste' option is set.  When
        'textwidth' is zero, 'wrapmargin' may be used.  See also
        'formatoptions' and |ins-textwidth|.
        When 'formatexpr' is set it will be used to break the line.
        NOTE: This option is set to 0 when 'compatible' is set.


'wrapmargin' 'wm'       number  (default 0) 
                        local to buffer
        Number of characters from the right window border where wrapping
        starts.  When typing text beyond this limit, an <EOL> will be inserted
        and inserting continues on the next line.
        Options that add a margin, such as 'number' and 'foldcolumn', cause
        the text width to be further reduced.  This is Vi compatible.
        When 'textwidth' is non-zero, this option is not used. 
        See also 'formatoptions' and |ins-textwidth|.  {Vi: works differently
        and less usefully}
如果你想自动换行长行并将它们发送到下一行,请尝试:
:set textwidth=0 
:set wrapmargin=0

15
有些插件似乎会覆盖这个设置。 - Yzmir Ramirez
1
“tw”和“wp”选项对我来说并没有真正起作用,所以我不得不选择“formatoptions”。https://github.com/ain/.vim/blob/28c71b23a3914ff298e5cc5f7aa2a82abc9f0fb9/.vimrc#L12 - Ain Tohvri
2
如果tw和wp仍然无法解决问题,请查看SU上的这篇文章,它帮助我解决了我的问题: http://superuser.com/questions/250112/textwidth-0-and-wrapwidth-0-in-vimrc-local-not-being-respected - Alien_SM
5
从我的.vimrc中移除filetype plugin on就解决了我的问题。 - user458541
2
使用这个答案,你不能使用 gq 命令手动调整段落以适应文本宽度。@Engineero 的应该是正确的。 - Dylanthepiguy

126

对我来说,其他答案都没有起作用(我不知道为什么)。

:set wrap! 对我有用了(我使用的是Windows版GVim)。


13
这解决了一个稍微不同的问题。 wrap 提供了文本自动换行的外观,但实际上并没有将文本断成新的行。你很可能启用了 wrap,所以 :set wrap! 可以将其切换关闭。你可以使用 :set wrap? 来检查当前值(即 wrapnowrap)。 - shadowtalker
2
这个解决方案在我的Mac OsX上有效。Vim版本为7.4。 - Shnkc
这对我有用,我首先更改了 textwidthwrapmargin,但实际上这迫使 vim 在没有任何换行的情况下更新和重新渲染。 - Kredns
2
叮叮叮!在neovim中工作了。 - Gjaa
适用于 neovim - Cirelli94
显示剩余3条评论

76

set formatoptions-=t 可以解决问题。set formatoptions+=t 会重新开启自动换行。

有关如何使用 formatoptions 的更多信息,请参见文档


3
谢谢!你的解决方案是唯一一个对我有用的。现在,当我进入插入模式时,vim不会再插入新行了。 - Geremia
3
好的,这是真正的解决方案。 - Russ Bateman
1
为了关闭Markdown文件中的自动换行,我必须使用set formatoptions=cq,正如@Ain_Tohvri在另一个答案下的评论中所解释的那样。 - Paul Rougieux

34

我为了防止vim自动折行,将以下两行代码添加到了我的.vimrc文件中:

set nowrap           " do not automatically wrap on load
set formatoptions-=t " do not automatically wrap text when typing

“set nowrap” 是在 neovim 中唯一对我有效的设置。 - ninja star

15

如果你想禁用文本自动换行,可以输入:set wrap!或将此命令附加到你的~/.vimrc文件中。


4
这不会影响自动插入真实换行符,这也是本问题讨论的内容。 - Arthur Tacca

11

可能是文本的宽度设定导致的,当文本长度达到一定程度时会自动换行。尝试调整文本宽度来解决。

:set tw=0

如果失败了,尝试调整例如:

:set wrap linebreak textwidth=0 

并且

:set virtualedit=insert

2
换行和断行不会将实际的行尾插入缓冲区,因此这似乎不是他的问题。 - A. Levy

0
打开 vimrc_example.vim(是的,这是Vim74中的文件)并将 textwidth 设置为0。

0

0

在 MacBook Pro 上,我在 .vimrc 文件中注释掉了这一行。

autocmd FileType text setlocal textwidth=78

所以它变成了

"  autocmd FileType text setlocal textwidth=78

.

我通过Homebrew安装了一个版本的vim。

这对所有的.txt文件都有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接