Vim Tips Wiki

Disable automatic comment insertion

Revision as of 14:13, November 8, 2012 by (Talk)

1,619pages on
this wiki
Tip 1361 Printable Monobook Previous Next

created 2006 · complexity basic · version 6.0

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 //, Vim may automatically insert // at the start of the new line. This tip explains how auto commenting can be disabled.

Disabling in general

If wanted, auto commenting can be disabled for all files with:

autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o

This sets up an auto command that fires after any filetype-specific plugin; the command removes the three flags from the 'formatoptions' 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:

:set formatoptions?


  1. include <iostream.h>
  2. include<stdlib.h>

using namespace std; class Bin_Add { public:

     Bin_Add() { current = -1;}
      int top(){return A[current];}
      int pop(){ return A[current--];} 
      void push(int x) {A[++current] = x;} 
      int isEmpty(){return ( current == -1 );} 
      void add();    


       int current; 
       int A[20]; 
       int x;
       int y; 

}; int main() {

   int n=0, a=0,b=0;
  Bin_Add b1,b2,b3,b_Sum; 
 cout <<"\n Pleas Enter the number of bits in each binary number = ";
 cin >> n;
 cout <<"\n Pleas Enter 1stbinary number bit by bit = \n";
 for (int i=0; i<n ;i++)
     cin >> a;

cout <<"\n Pleas Enter 1stbinary number bit by bit = \n";

 for (int i=0; i<n ;i++)
     cin >> b;

/*****************************************************************************************************/ /* ADDITION OF TWO BINARY NOS.*/

  int sum,carry=0,x1,x2;

while(!b1.isEmpty()||!b2.isEmpty()) { x1=x2=0; if(!b1.isEmpty())



sum = (x1+x2+carry)%2;

} if(carry==1)


cout << "\n Sum of binary number is = "; while(!b3.isEmpty()) { cout <<; b3.pop(); } cout << "\n "; /*****************************************************************************************************/ system("pause"); }

Overriding format options set by filetype plugin

If you have enabled file type plugins, you may notice that your format options still contain c, o, or r, depending on the filetype. One way to override these settings is by creating a file in your after directory.

For example, the command :filetype plugin sets formatoptions=lncrq for Perl files by default. You can modify that setting by creating a file named perl.vim with contents:

setlocal formatoptions-=c formatoptions-=r formatoptions-=o

The perl.vim file should be in this directory (which you may need to create): ~/.vim/after/ftplugin on Unix-based systems; or $HOME/vimfiles/after/ftplugin on Windows systems.



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:

set comments=

set noai nocin nosi inde=

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

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

set comments-=:// comments+=f://
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, 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. Use ":verbose set comments?" for this.
Fritzophrenic 16:50, January 18, 2012 (UTC)

Around Wikia's network

Random Wiki