Disable comment auto-insertion

Hello,

I am trying to prevent neovim from adding comment header when I’m editing a comment section.

Specifically, when I hit ‘A’+Enter on a comment line, I would very much like neovim not to insert anything on the new line.

I’ve searched the web and read what I believe to be the relevant sections of the doc, but nothing works.

Here’s what’s in my init.vim:

"prevent vim from doing things it wasn’t asked to
autocmd FileType * setlocal formatoptions-=
autocmd FileType * set formatoptions=
setlocal formatoptions=
set formatoptions=
set textwidth=0

Am I missing something or is this a bug?

Any help appreciated.

[EDIT]: it looks like setting formatoptions to nil is somehow overriden by something that loads when the filetype changes: after loading my file, the formatoptions have been replaced by croql (none of which are welcome)

You can find what changed an option with:

:verbose set formatoptions?
1 Like

Hi Benoit,

Thanks for taking the time to answer.

I’ve tried your setting in my init.vim and I do now see the current value of formatoptions in the status line (which is indeed not what is specified in my init file so something overwrites my settins).

However, it’s only showing me the current value, not what overwrote it or when it was overwritten.

Any further clue would be very welcome.

the verbose bit is not to go in your init file. its for you to paste in the cmd mode to evaluate. When you evaluate that it will show which script/file/files set it last.

Most likely its the ftplugin for your current file’s filetype

You are correct on both counts, and the offending plugin is:

Last set from /usr/share/nvim/runtime/ftplugin/c.vim line 22

But then, the question becomes: how do I override the settings forced by the plugin?

In traditional vim, doing:

autocmd FileType * set formatoptions=

in your init file overrides misbehaved plugins because the code is executed after the plugin loads.

neovim seems to do the opposite … which means you can’t override plugin behavior.

Is there a way to change settings After a plugin has loaded?

Is there a way to change settings After a plugin has loaded?

Yes. Create the file ~/.config/nvim/after/ftplugin/c.vim and write

setlocal formatoptions=

Obviously putting whatever values for formatoptions you want.

Anything in the after/ directory in your ~/.config/nvim folder is sourced after the built-in runtime files (hence the name). This also works for plugin/ and indent/ files.

I would recommend this:

create a file called lua.lua at ~/.config/nvim/after/ftplugin

then add:

vim.bo.formatoptions = 'jnqlr'

many people do:

:set formatoptions-=cro

Which means:

c       Auto-wrap comments using textwidth,
        inserting the current comment
        leader automatically.

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.  In case comment is unwanted
        in a specific place use CTRL-U to quickly delete
        it. i_CTRL-U

As you can se we have to leave eather r or o enabled and become aquanted whith CTRL-U enjoing the best of the two worlds, auto-commenting and the oposit :slight_smile:

Note: vim.bo means buffer option