VIM textwidth 没有效果

发布于 2024-10-20 01:09:36 字数 313 浏览 8 评论 0原文

这感觉像是一个愚蠢的问题,但我在互联网上(或在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

战皆罪 2024-10-27 01:09:36

尝试使用 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 buffer
  • gq means : reformat the text included in the motion
  • G 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.

吲‖鸣 2024-10-27 01:09:36

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

我的问题是默认的 ftplugin 禁用了 textwidth。

仅使用 (:set tw=79 && :set formatoptions+=t) 更新 .vimrc 是行不通的,因为 fplugins 是在 vimrc 之后获取的。

以下是我遵循的步骤:

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

:set formatoptions?
formatoptions=croql (note no 't')

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.

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

瞧!下次当您打开文件时,它会将文本宽度设置为您想要的字符。

作为调试辅助工具,您始终可以通过在命令前添加详细信息来检查哪个文件正在覆盖您的 vimrc 设置。因此,例如,如果我想检查谁最后更新了格式选项,我会输入

:verbose set formatoptions? 
formatoptions=croqlt
Last set from ~/.vim/after/ftplugin/fortan.vim

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)

:set formatoptions?
formatoptions=croql (note no 't')

2) create a filetype.vim file as suggested by vimdocs (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.

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

:verbose set formatoptions? 
formatoptions=croqlt
Last set from ~/.vim/after/ftplugin/fortan.vim
回忆追雨的时光 2024-10-27 01:09:36

打印什么

:set formatexpr?
:set indentexpr?
:set cindent?
:set filetype?
:set paste?
:filetype

其中至少有一个(我认为所有)都会覆盖 textwidth 的设置。

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

What does

:set formatexpr?
:set indentexpr?
:set cindent?
:set filetype?
:set paste?
:filetype

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.

爱冒险 2024-10-27 01:09:36

除非有要求,Vim 不会做任何事情。如果您有 t(仅适用于非注释)、c(仅适用于注释)或 textwidth 将对当前编辑的行产生影响两者都在 formatoptions 中(如果 a 不存在,则仅当您达到 textwidth 设置的边距时才会自动换行),或者如果您使用 gq 重新格式化文本。如果我没有记错的话,您可以设置这样的 formatexprformatprg 以便忽略 textwidth

Vim won't do anything unless requested. textwidth will have an effect for currently edited lines if you either have t (for non-comments only), c (for comments only) or both in formatoptions (if a is not present there, then it will autowrap only when you reach the margin set by textwidth), or if you use gq to reformat your text. If I am not mistaking, you can set such formatexpr or formatprg so that textwidth will be ignored.

2024-10-27 01:09:36
set formatoptions+=t

可能对你有帮助

set formatoptions+=t

This may help you

初懵 2024-10-27 01:09:36

我遇到了完全相同的问题,发现以下内容对我来说足够好地解决了该问题:

autocmd FileType python setlocal textwidth=79 formatoptions+=t

随意将 python 更改为您选择的文件类型(例如 * 或逗号分隔列表python、ruby、go、sh、javascript

有关formatoptions的详细信息,请参阅:h fo-table

I had this exact same issue and found the following solved the issue well enough for me:

autocmd FileType python setlocal textwidth=79 formatoptions+=t

Feel free to change python to be your file type of choice (e.g. * or comma separated list python,ruby,go,sh,javascript)

See :h fo-table for details of formatoptions

染墨丶若流云 2024-10-27 01:09:36

我正在使用 Neovim,也遇到了这个问题。在我的 init.vim 中将 textwidth 设置为 80 后,当我打开文件时,文本文件的内容不会改变。对于现有文本,我们必须使用 gggqG 手动设置其格式。

当您在一行中输入新字符时,textwidth 选项将起作用(如果字符数达到 80,您将自动换行)。

I am using Neovim and have met this issue too. After setting textwidth to 80 in my init.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 with gggqG.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文