Linux
Apache
MySQL
PHP

CSS
XHTML1.1
XML/RSS

Creative Commons

2008-05-28 10:14:04

Comments From VIM, Nevermore!

Has anyone else noticed how the newer versions of VIM attempt to be smart? Yeah, they think that just because you typed a comment on one line that you want to comment the next line, and the next, and the next. I don't know about you, but I'll tell the program which lines I want to comment, not the other way around. There has to be some way to tell VIM to never autocomment a line, ever.
First, you need to see what rules VIM is using for autocommenting. VI a random file and enter
:setlocal
and hit ENTER. In case you're VIM-dumb, the colon gets you into command mode, so don't put yourself in insert mode and then type the command. It won't work. If it did work, you'll see a list of local configuration parameters
:setlocal --- Local option values --- --autoread errorformat= keywordprg= scroll=11 comments=:# grepprg= makeprg= tags= define= include= path= formatoptions=tcql
or if your autocommenting is more ridiculous you may see
:setlocal --- Local option values --- --autoread grepprg= makeprg= tags= define= include= path= errorformat= keywordprg= scroll=11 comments=s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-,:# formatoptions=tcql
Whatever you see, you'll notice the line that begins with "comments."
My config looked like the first one, so I only had to find a way to remove :# from the list. Piece of cake.
In your home directory, make a .vimrc file (if you don't already have one) and put the setlocal command in there.
# cat ~/.vimrc setlocal comments-=:#
That tells it to keep the value of "comments," but remove :#. In my case, :# was the only value, so "comments" was empty afterward. You can modify this to your liking if you have the need of removing/adding more types of comments.

UPDATE 2009-01-26: From the comments, multiple people have said that the above way does not work. One such issue was due to vim's filetype detection, which happens AFTER your .vimrc gets sourced, nullifying what you wrote. To get around this and remove autocomments for all files, add this line to the end of your .vimrc:
au FileType * setlocal comments=


Back

38 comments


2008-05-29 08:39:32


John says...
Thank you very much! This was driving me crazy.

2008-05-29 10:45:31


slonkak says...
You are very welcome. That has bothered me since Fedora 8 and I just recently spent the time to research how to stop it.

2008-05-29 18:35:15


wildani says...
Changing the comments setting like that has other side effects.

The more focused fix is to change the formatoptions to remove all flags that let vim touch comments. That's the t, r, q, and o flags as of 7.0.
So,
:setlocal fo-=t fo-=r fo-=q fo-=o

will prevent vim from doing anything to your comments automatically.

Note that this disables even somewhat useful things like wrapping them at the edge of the screen when you type beyond it and inserting the comment leader at the beginning of the new line or allowing the use of text formatting commands on the comment without disturbing the comment indicators, even when the number of lines changes.

See :help fo-table for the rest of the flags and exact behavior descriptions.


2008-05-30 02:54:41


jose says...
Good tip, very useful, it was a hell for me in Ubuntu

2008-05-31 00:53:13


vince says...
Actually, I like the auto-commenting. I write a lot of
multi-line comments, and it is very convenient to have it
auto-indent and add the comment characters. It is quick enough
to just hit 'dd' or 'D' on the last line if you get an extra
comment you don't want. If you hit <esc> before hitting <enter>
on the last comment line you don't even have to do that. It
saves me more time than it costs me.

2008-05-31 10:46:29


slonkak says...
The biggest problem, for me, wasn't when I was typing code. It was when I would copy/paste code from the web browser. If the snippet I pasted was a shell script (i.e., started with a #!) VI would think that was a comment and would then autocomment EVERY SINGLE LINE after that. The entire block I pasted would be commented out. Talk about a royal pain.

2008-05-31 20:38:45


paste off says...
Try using ":set paste" to put VIM into paste mode. It won't auto-anything while in that mode. Use ":set nopaste" to return to regular mode.

Alternatively, put "set pastetoggle=<F11>" in your .vimrc to be able to use F11 to toggle between paste and regular modes... It's a huge time saver.

2008-06-01 17:55:11


Vim fan says...
Thank god some people who know what they're talking about replied to this. Before you post the solution to a problem, it's a good idea to make sure it's the best solution by doing a bit of research.

2008-06-01 18:28:46


slonkak says...
@vim fan I never said it was the best solution. I found a solution that works for me with what I do in VIM. Others, like yourself, may use VIM for more in-depth scenarios. Great, and thanks to everyone for giving alternate solutions.

I like it when VIM doesn't autocomment for me, and the side effects mentioned by @wildani don't apply. So for my use, changing the "comments" setting works great.

And thanks to everyone else, there are now many solutions listed that should help anyone, no matter how they use VIM. Welcome to the social aspect of the Internet.

2008-06-04 19:24:03


dr. jesse says...
"the colon gets you into command mode"

I thought the colon is the prefix for :subject :predicate :object.

What is this conflicting information?

Doesn't matter, real programmers would use visual studio anyway.

2008-06-06 19:34:33


kdude says...
Pasting in vim? I do ':!cat', and middle-button paste (ctrl-D closes). Or install the xclip package. I've got two local scripts 'xc' and 'xp' which are 'xclip -o' and 'xclip' respectively.

In which case: highlight text to be pasted. ':!xp' in vim. No mousing required (for the paste at least).

2008-06-16 08:29:59


Thoth says...
"Doesn't matter, real programmers would use visual studio anyway."

I know some programmers who use Visual Studio, most because they are too lazy to use anything other than what they were handed in college. Which the person most likely handing out Visual Studio was a failure of a programmer who couldn't make it in the real world so they ended up taking a remedial job at a college torturing students. And then I know a few who are truly talented at programming who hate Visual Studio but are forced by their job environment. But the real kicker is every time I've heard a debate among truly high end thinkers on what programming environment is king it's usually between VIM, EMACS, and Eclipse. I've never once heard a "real" programmer say that they prefer Visual Studio. But to each his own. Which is why I prefer VIM above all others, if you take the time to customize it for yourself you'll be far faster than any app that uses a mouse. Which I feel an obligatory link to the documentation's section on options should go here:

http://www.vim.org/htmldoc/options.html

Oh ya, and vim runs snappy on 15 year old hardware, try that on any of those other IDE's

2008-06-16 08:53:15


slonkak says...
@Thoth I totally agree with you. The person who left the comment about VS is actually a co-worker of mine who, using an alias, decided to post something that would make me angry. Instead, I decided to let it go... ;)

2008-06-16 14:26:04


Thoth says...
ya it sounded like bait, I just couldn't help but swallow hook, line, sinker, floater, pole, and fisherman

2008-06-28 12:21:42


mhd says...
putting ¨setlocal comments-=:#¨ in $HOME/.vimrc
doesn´t work for me. Vim in my slackware still auto comments for ´#´.Strange...

2008-06-28 12:58:00


slonkak says...
@mhd There are some weird characters in your comment. Did you maybe copy and paste from my article and get some funny characters? Have you tried typing it by hand? If you follow my initial instructions of editing a file and setting the comment variable while inside of vim, does it stop autocommenting? If you find something that works while in vim, you should be able to put that in your vimrc.

2008-07-31 11:14:13


Mike says...
I don't mind the auto-commenting stuff so much. What took me a while to find out (duh. . ) was to disable the auto-commenting while pasting. Via the simple :paste command (which I would recommend to hotkey). Again, a big duh on my part; but nice once I found.

2008-08-06 14:10:56


Motionblurrr says...
Dr. Jesse, Thoth, slonkak...

Thanks for the laugh... I saw the line about VS being "best" and immediately knew this could turn ugly very quickly. Thoth, your response was so perfect, yet apparently unnecessary. :)

2008-09-05 16:59:11


Wrightrocket says...
Using Fedora 9, and trying to edit a perl file, none of the above solutions worked for me. Luckily, some of the suggestions did get me on the right track. The file /usr/share/vim/vim71/ftplugin/perl.vim contained settings for formatoptions that made comments automatically get formatted. Here's an excerpt from that file:
" setlocal formatoptions+=crq
setlocal formatoptions=c
The line starting with " is a vim comment reflecting the original settings, and below it is my revised setting that works!

2008-09-05 20:15:25


Andrea says...
@wildani + slonkak:

If you just do ':set formatoptions-=ro', you only disable comment header auto-insertion, without losing other useful functionality carried by the 't' & 'q' flags.

Slonkak's comment on Vim trying to be "smart" is spot on: questionable automatisms like this one should be DISABLED by default and left for advanced users to discover and (possibly) enable, not the other way around!!

2008-10-07 06:33:14


john3i says...
Andrea is RIGHT on. Another example is a lot of distributions turn on colors for ls etc.

2009-01-12 14:40:45


Hani says...
Thanks Wrightrocket, your solution worked for me on Ubuntu 8.04 with vim 7.1

2009-01-17 18:32:37


Mark says...
Thank you!! Spent way too much time on this crap. Can't paste a perl file with #!/usr/bin/perl at the top without is commenting out the whole thing! Ridiculous!

2009-01-23 21:36:46


Rob says...
This is driving me crazy because it still won't persist in my $HOME/.vimrc file. I can do both;

:setlocal comments-=:#

and

:setlocal fo-=t fo-=r fo-=q fo-=o

inside vim and the work fine. If I put them in my ~/.vimrc (without the:, obviously) they are ignored. setlocal shows comments as having # again. Somewhere, another file is being sourced after my ~/.vimrc and overriding my changes which is an unforgivable abomination - users prefs should _always_ override system wide config. It's making me irrationally angry :-)

2009-01-23 22:35:35


Rob says...
It drove me so insane that I ended up doing weird things. I noticed that doing a -u ~/.vimrc causes the directives to be applied. Not wanting to spend any more time on it, I just made a bash alias to vi='/usr/bin/vim -u ~/.vimrc' and moved on with life. Massive pile of suckage, though. Nice to accomodate the 5% of users that find those default settings helpful at the price of annoying / confusing everyone else. It's like someone suddenly put some crap windows editor team in charge.

2009-01-24 13:24:13


slonkak says...
@Rob I'd still like to help you solve this. First off, edit your .vimrc and insert a new line at the top of the file. Just smash the keyboard so you get a bunch of random characters. Write quit, then try vi'ing a random file with no arguments (don't use your alias, or unalias it first). You should get an error. If you don't, then your .vimrc isn't getting read at all. Let me know which it is then we can figure it out from there. Also, what distro, version, and vim version are you using?

2009-01-24 22:34:26


Anonymous says...
I'm too obsessive compulsive to leave it alone ;)

- Running Ubuntu Intrepid / vim version 7.1.314
- Modifying ~/.vimrc with garbage does generate an error as expected

My ~/.vimrc contains;

syntax on
setlocal comments-=:#
setlocal fo-=t fo-=r fo-=q fo-=o
"colo robf

(I disabled my color scheme in testing).

The problem actually seems to be with the syntax related stuff. If I just vi fdsa (without my alias), setlocal doesn't have anything for comments=

However, if I like, vi fdsa.vim or fdsa.pl (without my alias) and add a comment (" or # respectively) followed by a newline, autocommenting happens and setlocal shows;

| comments=sO:" -,mO:" ,eO:"",:"
| formatoptions=croql

| comments=:#
| formatoptions=crq

Maddening. If I set either of those commands to remove auto commenting inside vi via :cmd, of course it works. Any thoughts of what to try next? Here's the output of vi -V asdf & vi -V asdf.pl

http://robfinch.net/~robf/screenlog.txt
http://robfinch.net/~robf/screenlog_perl.txt


2009-01-24 22:34:36


Rob says...
I'm too obsessive compulsive to leave it alone ;)

- Running Ubuntu Intrepid / vim version 7.1.314
- Modifying ~/.vimrc with garbage does generate an error as expected

My ~/.vimrc contains;

syntax on
setlocal comments-=:#
setlocal fo-=t fo-=r fo-=q fo-=o
"colo robf

(I disabled my color scheme in testing).

The problem actually seems to be with the syntax related stuff. If I just vi fdsa (without my alias), setlocal doesn't have anything for comments=

However, if I like, vi fdsa.vim or fdsa.pl (without my alias) and add a comment (" or # respectively) followed by a newline, autocommenting happens and setlocal shows;

| comments=sO:" -,mO:" ,eO:"",:"
| formatoptions=croql

| comments=:#
| formatoptions=crq

Maddening. If I set either of those commands to remove auto commenting inside vi via :cmd, of course it works. Any thoughts of what to try next? Here's the output of vi -V asdf & vi -V asdf.pl

http://robfinch.net/~robf/screenlog.txt
http://robfinch.net/~robf/screenlog_perl.txt


2009-01-24 23:14:06


slonkak says...
@Rob I think I got it. Turns out that when you have filetype detection turned on (it is by default) those scripts get sourced after your .vimrc (which is crap; NOTHING should be sourced _after_ a dotfile in your home dir). But, you can exploit the filetype detection by adding this line in your .vimrc (I put it last):

au FileType * setlocal comments-=:#

Normally, you'd make the asterisk "php" to only do this for PHP files, but you probably want this for everything, so use the asterisk.

2009-01-25 00:12:13


Rob says...
Kevin, you're a genius. That works!

I guess that was my complaint, that nothing should override a user supplied dot conf file.

So, for languages that use other strings for comments, is there a way to specify them after comments-=:# (having trouble finding the syntax if there is) or should you do like;

au FileType perl setlocal comments-=:#
au FileType vim setlocal comments-=:"

(That works for me).

Anyway, thanks for figuring this out, it's one of those problems that drove me crazy because it's my beloved vi(m) and it Should Just Work.

2009-01-25 11:04:03


slonkak says...
@Rob If you're like me and don't like any kind of autocommenting, I just do:

au FileType * setlocal comments=

...so that it's empty and nothing gets autocommented ever.

If you want to be specific, just make sure that whatever filetype you pick is the same as the file.vim name in vim's filetype directory. So, if you wanted to do it for .kevin files, and the vim file is kevin.vim, your line would be:

au FileType kevin setlocal comments-=:#

2009-01-25 16:37:07


Rob says...
Yeah, I pretty much despise auto commenting. I can't really understand who it's useful for.. maybe writing perl pod style documentation? My in-code comments are usually no more than 3-4 lines, hitting # isn't exactly a burden. Even if I was writing 20 commented lines I don't see it as being worth for what everyone else I've talked to is a useless, annoying feature.

If you statistically compared the amount of wasted backspaces -> newline keystrokes people have to do to start a non-commented line, I'm sure you'd find a huge comparative inefficiency.

au FileType * setlocal comments= it is, thanks again for your help!

Cheers

2009-03-20 16:13:24


Joe says...
You are my savior man!

2009-03-24 17:36:28


Ben says...
Wow what a lot of criticism! I've been turning back on autocommenting actually.

First of, to see your comments settings, type :
echo &comments

How about this setup? :
set comments=s0://,mb://,ex:
(insert space after ex: <--)
This way the next line autocomments unless the last character on the commented line happens to be a space -- pretty cool. You can give ex: the value you want for instance
set comments=s0://,mb://,ex:.
This way autocomments stops if comment ends with a fullstop (.)

This is is what should go in ~/.vimrc
au FileType c set comments=s0://,mb://,ex:
au FileType python set comments=s0:#,mb:#,ex:
au FileType ruby set comments=s0:#,mb:#,ex:

etc.

All the best

2009-03-24 17:39:58


Ben says...
There needs to be a backslash + a space after "ex:" in this example. It didn't post properly.


set comments=s0://,mb://,ex:[backslash][space]

2010-05-08 21:34:29


REnatho says...
Je suis tres bien et toi, watson me demende pour toi.au revoir.

2010-07-16 06:22:57


TJ says...
THANKS SO FRIGGEN MUCH!!! do VIM developers seriously think users are getting less capable of deciding how to format their files as they try to 'improve' vim - come on guys - its already improved - now stop this auto indent bull!

2011-12-30 14:59:27


ctapyxa says...
to banish "nopaste" completely (and for all users), add

set paste

to /etc/vimrc

You can flip it anytime, but the default mode will be "set paste" instead of "set nopaste". I think it should have been made default, anyway.

2013-03-13 04:04:17


Gryftir says...
To turn off, you need to disable the q option in formatoptions

:setlocal find your format options
then in your .vimrc
au Filetype * set formatoptions=
after the =, put your format options without the q

see http://vimdoc.sourceforge.net/htmldoc/change.html#format-comments

Post a comment!

Name:
Comment: