Auto-comment new line in Vim only for block comments
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 .cpp
) that comes with Vim. You can check the file-type for a buffer with :set ft?
.
The setting for continue comments after you start a new line comes from option 'comments'
, as pb2q said.
For .{cpp,h}
the default file-type is 'cpp', and the 'comment'
option is set at $VIMRUNTIME/ftplugin/c.vim
, as the cpp.vim
is in the same directory. From the c.vim
file:
" Set 'comments' to format dashed lists in comments.
setlocal comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,://
The comments
option is a list of {flags}:{string}
, and flags f
and O
avoid extending the comment new lines.
From the 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.
So create the file ~/.vim/after/ftplugin/c.vim
with
setlocal comments-=://
setlocal comments+=fO://
should solve the problem.
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://