Search code examples
vimcommentsautoformatting

Auto-comment new line in Vim only for block comments


When I start a new line at the end of a single-line comment in a .{cpp,h} file, vim is automatically commenting it. For example:

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

I'm not sure if this is a plugin or a setting. I can't see anything that looks like it'd do this in my ~/.vimrc, and the loaded plugins are listed below.

I like this for /* */-style multiline comments, but I don't want my single-line comments running over multiple lines by default.

Which setting (or plugin) does this, and can I turn it off for this comment type only?

:scriptnames gives this:


  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

Solution

  • au FileType c,cpp setlocal comments-=:// comments+=f://
    

    In your vimrc should do the trick for // without affecting block comments, in {cpp,h} files.

    To try it temporarily in the current buffer use:

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