VIM textwidth无效

Aeo*_*aut 22 unix vim text-editor neovim

这感觉就像一个愚蠢的问题,但我无法在互联网上找到答案(或在VIM帮助中).我在Mac OS X上使用的是VIM 7.2.我想做的就是用72个字符包裹我的行,但是这样做

:set textwidth=72 
Run Code Online (Sandbox Code Playgroud)

没有效果.正确设置了textwidth(我可以看到当我只查询":set textwidth"时),但是在设置textwidth之后我输入的现有行和新行都没有被包装.如果我开始换行,仍然不会换行.打开并关闭文件,无需更改.我也尝试过:set wrapmargin = 72(textwidth = 0),没有效果.

我在这里错过了什么?

Xav*_* T. 17

尝试gggqG将新文本宽度应用于整个缓冲区.

  • gg 意思是:转到缓冲区的开头
  • gq 表示:重新格式化动议中包含的文本
  • G 意思是:转到缓冲区的末尾

(如果格式选项设置正确,它将起作用,详见Zyx帖子)

另一方面,您还可以通过modeline在文件的开头或结尾添加a来显示宽度为72个字符的现有文本.看到:help modeline

有点像vim:tw=72应该工作.


Mik*_*kel 10

是什么

:set formatexpr?
:set indentexpr?
:set cindent?
:set filetype?
:set paste?
:filetype
Run Code Online (Sandbox Code Playgroud)

打印.

至少有一个(我认为所有这些)将覆盖设置textwidth.

例如,如果您正在编辑C源文件,则C缩进规则将覆盖textwidth.

  • 还试试`:设置粘贴?`.那个人也可以干涉. (2认同)

slo*_*art 9

我正在寻找同一个问题的答案,并且在我在VIM文档中找到解决方案之前不得不喋喋不休.所以,我想我会更新线程并节省其他时间.

我的问题是默认的ftplugin是禁用textwidth.

只是使用(:set tw=79 && :set formatoptions+=t)更新你的.vimrc是行不通的,因为fplugins来自vimrc之后.

以下是我遵循的步骤:

1)找出你当前的格式选项(在vim内)

:set formatoptions?
formatoptions=croql (note no 't')
Run Code Online (Sandbox Code Playgroud)

2)按照vimdocs的建议创建一个filetype.vim文件(http://vimdoc.sourceforge.net/htmldoc/filetype.html#ftplugin-overrule)

Overrule the settings after loading the global plugin.
You must create a new filetype plugin in a directory from the end of
'runtimepath'.  For Unix, for example, you could use this file:
    vim ~/.vim/after/ftplugin/fortran.vim
In this file you can change just those settings that you want to change.
Run Code Online (Sandbox Code Playgroud)

3)在该文件中添加行:set formatoptions+=t&& :set textwidth=79.

瞧!下次打开文件时,它会将textwidth设置为您想要的字符.

作为调试辅助工具,您可以通过在verbose前添加命令来检查哪个文件覆盖了您的vimrc设置.因此,例如,如果我想检查谁最后更新了formatoptions,我会输入

:verbose set formatoptions? 
formatoptions=croqlt
Last set from ~/.vim/after/ftplugin/fortan.vim
Run Code Online (Sandbox Code Playgroud)


ZyX*_*ZyX 7

除非有要求,否则Vim不会做任何事情.textwidth将有,如果你要么当前编辑行的效果t(仅适用于非评论),c或(仅适用于评论)都formatoptions(如果a不存在那里,那么它只会当你到达利润率设定autowrap textwidth),或如果您使用gq重新格式化文本.如果我没有弄错,你可以设定formatexprformatprg使textwidth将被忽略.