在Vim中自动注释新行仅用于块注释

sim*_*ont 16 vim comments autoformatting

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

// This is a comment<CR>
// | <- Cursor is moved to `|`, `//` is automatically inserted. 
Run Code Online (Sandbox Code Playgroud)

我不确定这是插件还是设置.我看不到任何看起来像是在我这样做的东西,~/.vimrc下面列出了加载的插件.

喜欢这种for- /* */style 多行注释,但我不希望我的单行注释默认在多行上运行.

哪个设置(或插件)可以执行此操作,是否可以仅为此注释类型将其关闭?

: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
Run Code Online (Sandbox Code Playgroud)

pb2*_*b2q 14

au FileType c,cpp setlocal comments-=:// comments+=f://
Run Code Online (Sandbox Code Playgroud)

在你的vimrc中,应该//在{cpp,h}文件中执行操作而不影响块注释.

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

:setlocal comments-=:// comments+=f://
Run Code Online (Sandbox Code Playgroud)

  • `comments + = f://`做什么? (2认同)

mMo*_*ntu 6

这种与特定文件类型相关的配置通常通过文件类型插件设置..cppVim附带的常见文件类型(例如)有许多文件类型.您可以使用以下命令检查缓冲区的文件类型:set ft?.

'comments'pb2q说,开始新行后继续注释的设置来自选项.对于.{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:*/,://
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

因此,创建该文件~/.vim/after/ftplugin/c.vim

  setlocal comments-=://
  setlocal comments+=fO://
Run Code Online (Sandbox Code Playgroud)

应该解决问题.