My formatoptions include cro.

I have a filetype (Stata .do files) in which the comment character is *. Anything after this character is considered a comment line.

In insert mode *<CR>* leads to

* <cursor>

However **<CR> leads to


My desired behavior, which seems to work in most other filetypes in my configuration, is that **<CR> leads to


What's wrong with this filetype and how can I fix it?

Another filetype example I found with similar behavior is Python. There if I do #<CR># I get


But if I do ##<CR> I get


Instead of what I would expect, which is


This seems to be the same behavior as with the Stata files except that in the Stata files vim also inserts a space after the *. In Python it doesn't add the space but still doesn't recognize ## as being a comment line.

  • 2
    You could be more specific - which language? Perhaps check if your language does not define another syntax rule for **, which disables the comment; or maybe ** simply means "start comment - end comment" and creates an empty comment in that language.
    – VanLaser
    Jan 25 '16 at 1:27
  • I added some specifics as well as another example. Can you explain how I would go about checking these things in vim? I'm not really sure where to find the settings that determine what vim considers to be a comment.
    – evencoil
    Jan 25 '16 at 13:30

You might have to set a custom comments. For me, comments in a Python file is:


With some experimenting, I found that, for Vim to insert # after lines with ##, I needed to use a three-piece comment (or part of one, anyway):

- Three-piece comments that have a start string, an end string, and optional
  lines in between.  The strings for the start, middle and end are different.
  An example is the C style comment:
         * this is a C comment

The start would be #, with nesting allowed:

  n     Nested comment.  Nesting with mixed parts is allowed.  If 'comments'
        is "n:),n:>" a line starting with "> ) >" is a comment.

But not requiring a blank space after (so ## would work), so omitting b:

  b     Blank (<Space>, <Tab> or <EOL>) required after {string}.

The middle would be #, without nesting, but inserting a space:

        When together with 's' or 'e': add {digit} amount of offset to an
        automatically inserted middle or end comment leader. The offset begins
        from a left alignment. See below for more details.


set comments+=sn:#,mb1:#

And now ##<CR> gives:

# _

Similarly, for your Stata file, you can set comments like so:

set comments+=ns:*,mb1:*

Check what it is first, and modify it appropriately.

  • I ended up using set comments=:# since I prefer that there be no immediate space after the # on the second line. (I neglected to indicate this in my question.) Thanks for the clear explanation!
    – evencoil
    Jan 25 '16 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.