禁用所有文件类型的自动注释

发布于 2024-09-09 15:57:51 字数 849 浏览 7 评论 0原文

我为我添加的一些rails vim插件打开了文件类型插件,但这样做的副作用似乎是现在所有文件类型都启用了自动注释(例如,如果我以#,下一行,无论是在插入模式下按 EnterO 等进入插入模式,也会得到一个 #< /代码>)。

我找到了指南来禁用自动评论formatoptions,并将以下内容添加到我的 .vimrc 中:

au FileType * setlocal formatoptions-=cro

但是,我仍然遇到问题 - 除非我明确 :source .vimrc,(或输入 setlocal .. . 直接),它没有生效。我确定是这种情况,因为 vim 的 ftplugins 用它们自己的选项覆盖了我的选项。

然后我找到了 第二个指南,其中讨论了使用 after ftplugin 脚本在 ftplugin 脚本之后进行更改已经运行,但是他们的解决方案是为 ~/.vim/after/ftplugin 中的每个文件类型创建到中心文件的符号链接,这对我来说似乎很混乱。

有没有办法创建通用的 after-ftplugin 脚本,或者我是否错误地处理了这个问题?任何帮助将不胜感激。

I turned on filetype plugin for some rails vim plugins I added, but a side effect of this seems to be that now autocommenting has been enabled in all filetypes (for instance, if I start a line with #, the next line, either by Enter in insert mode or O, etc. to enter insert mode, will also get a #).

I found a guide to disabling the auto-commenting formatoptions, and added the following to my .vimrc:

au FileType * setlocal formatoptions-=cro

However, I am still running into problems -- unless I explicitly :source .vimrc, (or enter the setlocal ... directly), it is not taking effect. I determined that this is the case because vim's ftplugins are overriding my options with their own.

I then found a second guide which talks about using an after ftplugin script to make changes after the ftplugin scripts have run, however their solution is to create symlinks for every single filetype in ~/.vim/after/ftplugin to a central file, and this seems to be kludgy to me.

Is there any way to create a generic after-ftplugin script or am I approaching this problem incorrectly? Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

披肩女神 2024-09-16 15:57:51

“之后”插件怎么样?在 ~/.vim/after/plugin/ 中创建一个名为 noAutoComments.vim (或其他名称)的文件并将你的 autocmd 放入其中?

编辑:

这有效的原因是什么?我只是在这里猜测,但我有一种感觉 ~/.vimrc 文件中的 autocmd 被其他一些文件删除(但在“after”之前)文件正在获取)。

我最终删除了我的 ~/.vim 目录,并用以下 3 行替换了我的 ~/.vimrc

filetype plugin on
syntax on
au FileType * setlocal formatoptions-=cro

在我的 ~/.vimrc 中只有这些行 并且没有 ~/.vim/ 目录,autocmd 似乎按预期工作(Vim 7.1)。

对于我编辑的任何文件:

:verbose set formatoptions?
formatoptions=ql
      Last set from ~/.vimrc

但是,我尚未确定哪个文件(插件)导致了此问题。

How about an "after" plugin? Create a file in ~/.vim/after/plugin/ called noAutoComments.vim (or whatever) and place your autocmd in that?

Edit:

The reason this works? I'm only guessing here, but I have a feeling that the autocmd in the ~/.vimrc file is getting removed by some other file (but before the "after" files are getting sourced).

I ended up removing my ~/.vim directory and replaced my ~/.vimrc with the following 3 lines:

filetype plugin on
syntax on
au FileType * setlocal formatoptions-=cro

With only these lines in my ~/.vimrc and no ~/.vim/ directory, the autocmd seems to work as expected (Vim 7.1).

For any file that I edit:

:verbose set formatoptions?
formatoptions=ql
      Last set from ~/.vimrc

I have yet to determine what file (plugin) is causing this issue however.

浮世清欢 2024-09-16 15:57:51

我做了更多调查,似乎 .vimrc 文件中 autocmd 的位置决定了 formatoptions 是否会被 vim 的 ftplugins 覆盖。使用 vim --noplugin 禁用所有外部插件,我发现以下结果:

如果我的 vimrc 看起来像:

au FileType * setl fo-=cro
filetype plugin indent on

:verbose set fo? 的结果是:

formatoptions=croql
  Last set from /usr/share/vim/vim72/ftplugin/ruby.vim

但是,如果我的 vimrc 中的行是相反的:

filetype plugin indent on
au FileType * setl fo-=cro

:verbose set fo? 的结果是:

formatoptions=ql
  Last set from ~/.vimrc

... 这是所需的结果。所以看来启用文件类型插件后需要指定 autocmd

I've done some more investigation and it seems that the location of my autocmd within my .vimrc file determines if formatoptions will be overridden by vim's ftplugins or not. Using vim --noplugin to disable all external plugins, I found the following results:

If my vimrc looks like:

au FileType * setl fo-=cro
filetype plugin indent on

The result of :verbose set fo? is:

formatoptions=croql
  Last set from /usr/share/vim/vim72/ftplugin/ruby.vim

However, if the lines in my vimrc are reversed:

filetype plugin indent on
au FileType * setl fo-=cro

The result of :verbose set fo? is:

formatoptions=ql
  Last set from ~/.vimrc

... which is the desired result. So it seems that the autocmd needs to be specified after filetype plugins are enabled.

余生再见 2024-09-16 15:57:51

这可能不会生效的另一个原因...

来自 :he :set-=:

            When the option is a list of flags, {value} must be
            exactly as they appear in the option.  Remove flags
            one by one to avoid problems.

我有,

    " Turn off auto-commenting
    au FileType * setlocal formatoptions-=c
    au FileType * setlocal formatoptions-=r
    au FileType * setlocal formatoptions-=o

因为我遇到过这个。

Another reason this might not be taking effect...

From :he :set-=:

            When the option is a list of flags, {value} must be
            exactly as they appear in the option.  Remove flags
            one by one to avoid problems.

I have

    " Turn off auto-commenting
    au FileType * setlocal formatoptions-=c
    au FileType * setlocal formatoptions-=r
    au FileType * setlocal formatoptions-=o

because I've run into this.

凉风有信 2024-09-16 15:57:51

如果您找到正确的一个,使用各种 autocmd 事件之一来设置配置选项应该可以工作,但我首先运行:

:verbose set formatoptions?

这将告诉您设置该选项的位置,这可能会使其生效更容易确定要使用哪个 autocmd。或者,如果您不介意一些轻微的黑客行为,我可能会做的方式就是找出它在插件中的设置位置并注释掉该行(并记下它,以防您升级插件)。您还可以联系该插件的作者并要求他们将其设为可配置选项。

对于可用的 autocmd 事件,请阅读以下内容:

:help {event}

Using one of the various autocmd events to set the configuration option should work if you find the right one, but I'd start by running:

:verbose set formatoptions?

This will tell you where the option was set, which may make it easier to determine which autocmd to use. Alternatively, if you don't mind a bit of minor hacking, the way I'd probably do it is just to find out where it's set in the plugin and comment out that line (and make a note of it in case you ever upgrade the plugin). You could also contact the plugin's author and ask them to make it a configurable option.

For the available autocmd events, read this:

:help {event}
久夏青 2024-09-16 15:57:51

我尝试过许多人提出的解决方案,但没有一个对我有用,但我发现了一个非常简单的解决方法,即在您的 ~/.bash_aliases 中:

    # vim without auto comment
    alias vi="vi +'set fo-=cro'"

I have tried solutions proposed by many, but none of them worked for me, but I found one very simple workaround, namely, in your ~/.bash_aliases:

    # vim without auto comment
    alias vi="vi +'set fo-=cro'"
梦里泪两行 2024-09-16 15:57:51

我一直在努力解决这个问题,最后我使用了以下几行:

syntax on
filetype on
filetype plugin on
au FileType * setlocal formatoptions-=cro

我认为这里的关键是 autocmd 位于 filetype 插件 之后。

I was struggling with this issue and I finally works with the following lines:

syntax on
filetype on
filetype plugin on
au FileType * setlocal formatoptions-=cro

I think the key here is that the autocmd is place after the filetype plugin on.

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