【问题标题】:Auto-comment new line in Vim only for block comments在 Vim 中自动注释新行仅用于块注释
【发布时间】:2012-05-30 09:08:30
【问题描述】:

当我在 .{cpp,h} 文件中的单行注释末尾开始新行时,vim 会自动对其进行注释。例如:

// This is a comment<CR>
// | <- Cursor is moved to `|`, `//` is automatically inserted. 

我不确定这是插件还是设置。我在~/.vimrc 中看不到任何看起来会执行此操作的内容,加载的插件如下所列。

喜欢 /* */-style multiline cmets,但我不希望我的 single-line cmets 运行在多个默认为行。

哪个设置(或插件)可以做到这一点,我可以关闭它只针对这种评论类型吗?

:scriptnames 给出了这个:


  1: /Users/simont/.vimrc
  2: /usr/local/share/vim/vim73/syntax/syntax.vim
  3: /usr/local/share/vim/vim73/syntax/synload.vim
  4: /usr/local/share/vim/vim73/syntax/syncolor.vim
  5: /usr/local/share/vim/vim73/filetype.vim
  6: /usr/local/share/vim/vim73/ftplugin.vim
  7: /usr/local/share/vim/vim73/syntax/nosyntax.vim
  8: /Users/simont/repositories/config-files/vim/colors/solarized.vim
  9: /usr/local/share/vim/vim73/plugin/getscriptPlugin.vim
 10: /usr/local/share/vim/vim73/plugin/gzip.vim
 11: /usr/local/share/vim/vim73/plugin/matchparen.vim
 12: /usr/local/share/vim/vim73/plugin/netrwPlugin.vim
 13: /usr/local/share/vim/vim73/plugin/rrhelper.vim
 14: /usr/local/share/vim/vim73/plugin/spellfile.vim
 15: /usr/local/share/vim/vim73/plugin/tarPlugin.vim
 16: /usr/local/share/vim/vim73/plugin/tohtml.vim
 17: /usr/local/share/vim/vim73/plugin/vimballPlugin.vim
 18: /usr/local/share/vim/vim73/plugin/zipPlugin.vim
 19: /usr/local/share/vim/vim73/scripts.vim
 20: /usr/local/share/vim/vim73/ftplugin/vim.vim
 21: /usr/local/share/vim/vim73/syntax/vim.vim

【问题讨论】:

    标签: vim comments autoformatting


    【解决方案1】:
    au FileType c,cpp setlocal comments-=:// comments+=f://
    

    在你的 vimrc 中应该对 // 进行处理而不影响 {cpp,h} 文件中的块 cmets。

    要在当前缓冲区中临时尝试使用:

    :setlocal comments-=:// comments+=f://
    

    【讨论】:

    • comments+=f:// 是做什么的?
    【解决方案2】:

    这种与特定文件类型相关的配置,通常是通过文件类型插件设置的。 Vim 附带了许多用于常见文件类型的文件类型(例如.cpp)。您可以使用:set ft? 检查缓冲区的文件类型。

    正如 pb2q 所说,开始新行后继续 cmets 的设置来自选项'comments'。 对于.{cpp,h},默认文件类型为'cpp','comment' 选项设置为$VIMRUNTIME/ftplugin/c.vim,因为cpp.vim 位于同一目录中。来自c.vim 文件:

      " Set 'comments' to format dashed lists in comments.
      setlocal comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,://
    

    comments 选项是{flags}:{string} 的列表,标志fO 避免将注释扩展为新行。

    来自Vim FAQ

      You can use an autocommand triggered on the FileType event:
    
          au Filetype * set formatoptions=xyz
    
      This should at least be after "filetype on" in your vimrc. Best is to put
      it in your "myfiletypefile" file, so that it's always last.
    
    
      If you want to override a setting for a particular filetype, then create a
      file with the same name as the original filetype plugin in the
      ~/.vim/after/ftplugin directory For example, to override a setting in the
      c.vim filetype plugin, create a c.vim file in the ~/.vim/after/ftplugin
      directory and add your preferences in this file.
    

    所以用

    创建文件~/.vim/after/ftplugin/c.vim
      setlocal comments-=://
      setlocal comments+=fO://
    

    应该能解决问题。

    【讨论】:

      【解决方案3】:

      您可以使用在 FileType 事件上触发的自动命令:

        au Filetype * set formatoptions=xyz
      

      这至少应该在你的 vimrc 中的 "filetype on" 之后。最好是放 它在你的“myfiletypefile”文件中,所以它总是最后的。

      如果您想覆盖特定文件类型的设置,请创建一个 与原始文件类型插件同名的文件 ~/.vim/after/ftplugin 目录 例如,要覆盖 c.vim 文件类型插件,在 ~/.vim/after/ftplugin 中创建一个 c.vim 文件 目录并在此文件中添加您的首选项。 所以用

      创建文件~/.vim/after/ftplugin/c.vim

      setlocal cmets-=:// setlocal cmets+=fO://

      【讨论】:

        猜你喜欢
        • 2019-02-21
        • 2012-01-16
        • 2011-01-20
        • 2012-06-12
        • 2011-03-18
        • 1970-01-01
        • 2014-05-07
        • 2014-01-15
        • 1970-01-01
        相关资源
        最近更新 更多