Question: Is there a way to control the indentation + comment leader input, when adding a new line?

Details: I am trying to set formatoptions-=cro when inserting a new line (in specific syntax region). I tried using the autocommands InsertEnter and InsertCharPre, but both are too generic for my use case and I cannot figure out how to only operate on entering an <Enter> when in insert and o/O when in normal mode.

(InsertCharPre does not trigger on new line insertions, and InsertEnter also triggers on typing a/A/i/I.) What am I missing? Any pointers will be greatly appreciated. :)

Edit: I forgot to mention, that my intention is to dynamically add/suppress a comment-continuation insertion on new line insertions (depending on the syntax region).

  • If there hadn't been an option to handle this per your answer then rather than try to detect newline insertions I think a promising route would be 'indentexpr'. You could have a function decide whether to pass control to regular indentation methods or to swallow it. Would require a bit of work to detect everything but it's less hacky than newline fishing, IMO.
    – B Layer
    Commented Jul 13, 2018 at 3:44

1 Answer 1


tl;dr: I was looking for :set comments+=f:{string}.

Details: My usecase is, that every time I have the following style starting comment (in a javascript file) and I type <Enter>/o/O it should not yield an auto-indented + auto-commented next line on enter:

 * <-- I wanted to suppress this

The solution to this was to :set comments+=f:/*::, which sets the comment to not be continued with any mb:{string} comments on the next line. See :help comments for further details.

Note: If you still have an answer to "How do I trigger a function on adding a new line?" feel free to answer. ✌️

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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