在Vim中自动添加新行,仅用于块评论

问题描述 投票:16回答:3

当我在单行注释的结尾处开始新的一行时,在一个 .{cpp,h} In your vimrc should do the trick for vim without affecting block comments, in {cpp,h} files.

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

To try it temporarily in the current buffer use:~/.vimrc

This kind of configuration, that is related to specific file types, are normally set through a file-type plugin. There is a number of file-types for common filetypes (such as ) that comes with Vim. You can check the file-type for a buffer with /* */.The setting for continue comments after you start a new line comes from option , as pb2q said.For the default file-type is 'cpp', and the option is set at

, as the is in the same directory. From the file:

:scriptnamesThe


  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
option is a list of
vim comments autoformatting
3个回答
14
投票
au FileType c,cpp setlocal comments-=:// comments+=f://

From the //Vim FAQ

:

:setlocal comments-=:// comments+=f://
So create the file

6
投票

should solve the problem..cpp :set ft?

You can use an autocommand triggered on the FileType event:'comments'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..{cpp,h}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 ~ 'comment' 是自动注释它。比如说。$VIMRUNTIME/ftplugin/c.vim我不知道这是一个插件还是一个设置 I'm not sure if this is a plugin or a setting. 我看不到任何看起来像这样做的东西,在我的 cpp.vim,加载的插件如下。c.vimI

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

喜欢comments 此为 {flags}:{string}-式 f多行O 评论,但我不希望我的

单行 缺省情况下,评论会超过多行。哪种设置(或插件)会这样做,我可以把它关掉吗?

  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://

给出了这样的结果。


0
投票

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

  au Filetype * set formatoptions=xyz

/ .vimafterftplugin目录 例如,要覆盖c.vim文件类型插件的设置,在~.vimafterftplugin目录下创建一个c.vim文件,并在这个文件中添加你的偏好。

setlocal comments-=:/ setlocal comments+=fO:/

© www.soinside.com 2019 - 2024. All rights reserved.