VIM textwidth 没有效果
这感觉像是一个愚蠢的问题,但我在互联网上(或在 VIM 帮助中)找不到答案。我在 Mac OS X 上使用 VIM 7.2。我想做的就是将行换行为 72 个字符,但这样做
:set textwidth=72
没有任何效果。文本宽度设置正确(我可以看到,当我只查询“:set textwidth”时),但设置文本宽度后现有的行和我键入的新行都不会被换行。如果我开始一个新行,仍然不会换行。打开并关闭文件,没有任何变化。我也尝试过 :set wrapmargin=72 (textwidth=0),但没有效果。
我在这里缺少什么?
This feels like a dumb question, but I can't find an answer on the Internet (or in VIM help). I'm using VIM 7.2 on Mac OS X. All I want to do is wrap my lines at 72 characters, but doing
:set textwidth=72
has no effect. The textwidth is being set correctly (I can see that when I just query ":set textwidth"), but neither existing lines nor new lines that I type after setting textwidth get wrapped. If I start a new line, still doesn't wrap. Open and close the file, no change. I've also tried :set wrapmargin=72 (with textwidth=0), with no effect.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试使用 gggqG 将新的文本宽度应用于整个缓冲区。
gg
表示:转到缓冲区的开头gq
表示:重新格式化运动中包含的文本G
表示:转到缓冲区的末尾(如果格式选项设置正确,它将起作用,如 Zyx 帖子中所述)
另一方面,您还可以通过在文件的开头或结尾。请参阅
:help modeline
。像
vim:tw=72
这样的东西应该可以工作。Try
gggqG
to apply the new text width to the whole buffer.gg
means : go to the beginning of buffergq
means : reformat the text included in the motionG
means : go to the end of the buffer(It will work if the format options are correctly set, as detailed in Zyx post)
On the other hand, you could also force your existing text files to a width of 72 characters by adding a
modeline
at the beginning or end of your file. See:help modeline
.Something like
vim:tw=72
should work.我一直在寻找同一问题的答案,并且在 VIM 文档中找到解决方案之前不得不四处寻找。所以,我想我会更新线程并节省其他人的时间。
我的问题是默认的 ftplugin 禁用了 textwidth。
仅使用 (
:set tw=79 && :set formatoptions+=t
) 更新 .vimrc 是行不通的,因为 fplugins 是在 vimrc 之后获取的。以下是我遵循的步骤:
1)找出当前的格式选项(在vim中)
2)按照vimdocs的建议创建一个filetype.vim文件(http://vimdoc.sourceforge.net/htmldoc/filetype.html#ftplugin-overrule)
3) 添加行
:set formatoptions+= t
&&:set textwidth=79
在该文件中。瞧!下次当您打开文件时,它会将文本宽度设置为您想要的字符。
作为调试辅助工具,您始终可以通过在命令前添加详细信息来检查哪个文件正在覆盖您的 vimrc 设置。因此,例如,如果我想检查谁最后更新了格式选项,我会输入
I was looking for an answer to the same question and had to scramble around a bit before I found the solution in the VIM docs. So, i thought i will update the thread and save others the time.
The problem in my case was that the default ftplugin was disabling textwidth.
Just updating your .vimrc with (
:set tw=79 && :set formatoptions+=t
) won't work since the fplugins are sourced after vimrc.Here are the steps that i followed:
1) find out what your current formatoptions (inside vim)
2) create a filetype.vim file as suggested by vimdocs (http://vimdoc.sourceforge.net/htmldoc/filetype.html#ftplugin-overrule)
3) add the line
:set formatoptions+=t
&&:set textwidth=79
in that file.Voila! next time when you open the file it will set the textwidth to your desired characters.
As a debugging aid you can always check which file is overriding your vimrc setting by prepending your command with verbose. So for e.g. if i want to check who updated the formatoptions last, i would type
打印什么
。
其中至少有一个(我认为所有)都会覆盖
textwidth
的设置。例如,如果您正在编辑 C 源文件,则 C 缩进规则将覆盖
textwidth
。What does
print.
At least one of those (and I think all of them) will override the setting for
textwidth
.For example, if you're editing a C source file, the C indent rules override
textwidth
.除非有要求,Vim 不会做任何事情。如果您有
t
(仅适用于非注释)、c
(仅适用于注释)或textwidth
将对当前编辑的行产生影响两者都在formatoptions
中(如果a
不存在,则仅当您达到textwidth
设置的边距时才会自动换行),或者如果您使用gq
重新格式化文本。如果我没有记错的话,您可以设置这样的formatexpr
或formatprg
以便忽略textwidth
。Vim won't do anything unless requested.
textwidth
will have an effect for currently edited lines if you either havet
(for non-comments only),c
(for comments only) or both informatoptions
(ifa
is not present there, then it will autowrap only when you reach the margin set bytextwidth
), or if you usegq
to reformat your text. If I am not mistaking, you can set suchformatexpr
orformatprg
so thattextwidth
will be ignored.这可能对你有帮助
This may help you
我遇到了完全相同的问题,发现以下内容对我来说足够好地解决了该问题:
随意将
python
更改为您选择的文件类型(例如*
或逗号分隔列表python、ruby、go、sh、javascript
)I had this exact same issue and found the following solved the issue well enough for me:
Feel free to change
python
to be your file type of choice (e.g.*
or comma separated listpython,ruby,go,sh,javascript
)我正在使用 Neovim,也遇到了这个问题。在我的
init.vim
中将textwidth
设置为 80 后,当我打开文件时,文本文件的内容不会改变。对于现有文本,我们必须使用gggqG
手动设置其格式。当您在一行中输入新字符时,
textwidth
选项将起作用(如果字符数达到 80,您将自动换行)。I am using Neovim and have met this issue too. After setting
textwidth
to 80 in myinit.vim
, the content of a text file does not change when I open the file. For the existing text, we have to manually format it withgggqG
.When you type new characters in a line, the
textwidth
option will work (you will get a linebreak automatically if the character number reaches 80).