So, I started using NeoVim a little while ago and discovered a rather annoying quirk when using C++-style single-line comments // in source code of any language. Specifically, when you create a new line after a line which starts with //, either by pressing enter in Insert Mode or by pressing o in Normal Mode, the newly created line will also be pre-formatted to begin with //

So for

//A single line comment

creating a line after it will produce

//A single line comment

with the cursor beginning Insert Mode after the second //

So far as I can tell, this applies to all languages that support single-line comments. It's rather annoying since the whole point of a single-line comment is that they're, well, a single line. This is likely just some new default option in NeoVim or something to do with the syntax files NeoVim uses compared to OG Vim but, regardless, how do I disable this "feature" so that creating a new line after a single-line comment won't automatically be prefixed with an unwanted //.

Essentially, when I press o in Normal Mode or press enter in Insert Mode on the following line:

//A single line comment

I should end up with:

//A single line comment

  • 1
    For anyone else wondering, I was able to fix this for all file types by adding autocmd! BufEnter * set fo-=r fo-=o (Note the formatting is actually rather specific for using autocmd this way; omitting the exclamation mark or putting spaces before or after the -= causes problems.)
    – Anadian
    Commented Apr 20, 2019 at 5:12

1 Answer 1

set fo-=r fo -=o
                    *'formatoptions'* *'fo'*
'formatoptions' 'fo'    string (default: "tcqj", Vi default: "vt")
            local to buffer
    This is a sequence of letters which describes how automatic
    formatting is to be done.  See |fo-table|.  When the 'paste' option is
    on, no formatting is done (like 'formatoptions' is empty).  Commas can
    be inserted for readability.
    To avoid problems with flags that are added in the future, use the
    "+=" and "-=" feature of ":set" |add-option-flags|.
r   Automatically insert the current comment leader after hitting
    <Enter> in Insert mode.
o   Automatically insert the current comment leader after hitting 'o' or
    'O' in Normal mode.

Note that this option is local to buffer, lots of ftplugin changes this option, you can use verbose set formatoption to check where it was last changed.

Most of time you need to put set fo-=r fo -=o in after/ftplugin/your_file_type.vim to override it. check :h after-directory

If you want to apply it to all kinds of files, you need to create an autocmd and make sure it's created after filetype plugon on, check here for detail.


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