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
Show activity on this post. To permanently disable this behavior, add autocmd FileType * set formatoptions-=cro in your . vimrc / init.
Using the up and down arrow key, highlight the lines you wish to comment out. Once you have the lines selected, press the SHIFT + I keys to enter insert mode. Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.
Hit Ctrl + q in GVIM or Ctrl + v in VIM, then go down to select first character on the lines to comment out. Then press c , and add the comment character.
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://
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With