4

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

**
<cursor>

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

**
*<cursor>

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

#
#<cursor>

But if I do ##<CR> I get

##
<cursor>

Instead of what I would expect, which is

##
#<cursor>

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.

muru
  • 24,838
  • 8
  • 82
  • 143
evencoil
  • 245
  • 1
  • 8
  • 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 01: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

1 Answers1

6

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

comments=b:#,fb:-

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:

  {digits}
        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.

So:

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.

muru
  • 24,838
  • 8
  • 82
  • 143
  • 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