Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
FANDOM
Explore
Current Wiki
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Disable automatic comment insertion
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Disable automatic comment insertion
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{TipImported |id=1361 |previous=1359 |next=1363 |created=2006 |complexity=basic |author= |version=6.0 |rating=170/47 |category1=C |category2=C++ |category3=Options }} Auto-comment allows easy insertion of additional lines of comments. For example, in a C++ file, if you insert a blank line after a comment line beginning with <code>//</code>, Vim may automatically insert <code>//</code> at the start of the new line. This tip explains how auto commenting can be disabled. == Disabling temporarily for the current session == To disable it just once for the current session: <code>:set formatoptions-=c formatoptions-=r formatoptions-=o</code> ==Disabling in general== To disable for all files and sessions, use this: <pre> autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o </pre> This sets up an auto command that fires after any filetype-specific plugin; the command removes the three flags from the <code>'formatoptions'</code> option that control the automatic insertion of comments. With this in your [[vimrc]], a comment character will not be automatically inserted in the next line under any situation. Use this command to check your format options: <pre> :set formatoptions? </pre> ==C++ single-line comments== Many C++ and C programs use <code>//</code>-style single line comments, like this: <pre> // This is a comment. </pre> When file type detection is enabled (for example, with <code>filetype plugin indent on</code> in your vimrc; see [[Keep your vimrc file clean|here]]), the file <code>$VIMRUNTIME/ftplugin/c.vim</code> is sourced when you edit a C or C++ file. The <code>c.vim</code> script sets the <code>'comments'</code> option for the local buffer (that is, for the C or C++ file). One result is that when the cursor is in the <code>// This is a comment.</code> line, pressing Enter will automatically insert the comment string (<code>//</code>) at the beginning of the next line. It is possible to disable this action, while leaving other auto-commenting features unchanged. To do this, create the file <code>~/.vim/after/ftplugin/c.vim</code> (Unix) or <code>$HOME/vimfiles/after/ftplugin/c.vim</code> (Windows), creating the directories if needed. The file contains these lines: <pre> setlocal comments-=:// setlocal comments+=f:// </pre> When you edit a C or C++ file, the first <code>c.vim</code> (in <code>$VIMRUNTIME/ftplugin</code>) will be sourced, then the second <code>c.vim</code> (in <code>after/ftplugin</code>) will be sourced. The second file modifies the <code>'comments'</code> option by removing (<code>-=</code>) the <code>://</code> component, then adding (<code>+=</code>) the <code>f://</code> component. The <code>f</code> causes the <code>//</code> to be used for a comment only on the first line, that is, Vim will not try to insert <code>//</code> on following lines. Creating the second <code>c.vim</code> is the recommended procedure, but an alternative would be to create an auto command by placing the following line in your [[vimrc]]: <pre> au FileType c,cpp setlocal comments-=:// comments+=f:// </pre> This auto command will be executed after the <code>c</code> or <code>cpp</code> file types are set. The command performs the same modification to the <code>'comments'</code> option as before, but uses the shortcut of placing the two operations in a single command. ==Overriding format options set by filetype plugin== If you have enabled [[file type plugins]], you may notice that your format options still contain <code>c</code>, <code>o</code>, or <code>r</code>, depending on the filetype. One way to override these settings is by creating a file in your <code>after</code> directory. For example, the command <code>:filetype plugin</code> sets <code>formatoptions=lncrq</code> for Perl files by default. You can modify that setting by creating a file named <code>perl.vim</code> with contents: <pre> setlocal formatoptions-=c formatoptions-=r formatoptions-=o </pre> The <code>perl.vim</code> file should be in this directory (which you may need to create): <code>~/.vim/after/ftplugin</code> on Unix-based systems; or <code>$HOME/vimfiles/after/ftplugin</code> on Windows systems. ==References== *{{help|'formatoptions'}} *{{help|fo-table}} *{{help|format-comments}} ==Comments== This leaves the cursor on the next line in the fourth position. Any suggestions on how to leave the cursor in the first position of the next line? ---- Doesn't work with my VIM installation. Editing ~/.vimrc setting the following (at the bottom of the file) has no effect: <pre> set comments= set noai nocin nosi inde= au FileType c,cpp setlocal comments-=:// comments+=f:// setlocal comments-=:// comments+=f:// set comments-=:// comments+=f:// </pre> :You don't say what you're trying to accomplish, but it looks like youre attempting to remove all automatic indentation, keep automatic insertion of /*...*/ comments in C and C++ code, and remove automatic insertion of //... comments in C and C++ code. :First, Vim does not automatically indent anything unless you tell it to, with a "filetype indent on" or a "filetype indent plugin on" in your .vimrc. Remove the "indent" keyword and you should not get any automatic indenting. Although it may be a good idea to keep your "set noai..." line to make sure you default to no indents, unless you disable the automatic indent setup per filetype, the filetype indent plugins will override these every time you load a file. :Your FileType autocmd should work fine, [[File type plugins|assuming you have automatic filetype detection turned on]], and should also render the following setlocal and set commands unnecessary. You say it has no effect. What do you try (step by step) to see that it has no effect? What do you see? What do you expect to see? :Something to check to point you in the right direction, if the option is not getting set as you expect, is to see where it was last set. [[Debug unexpected option settings|Use ":verbose set comments?" for this]]. :[[User:Fritzophrenic|Fritzophrenic]] 16:50, January 18, 2012 (UTC)
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Help
(
view source
)
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab