Options "formatoptions" not working when put in "init.lua"

I want to set an option so that neovim stops automatically commenting the next line when I press Enter while in insert mode on a commented line.
After reading the docs, I found that I need to remove a flag from the option : “formatoptions”
I tried entering the following → “:set formatoptions-=r” and it worked.
Then I tried to make the change permanent by adding the following line in my init.lua →
This code doesn’t throw any errors, but after sourcing the new configuration, nothing gets changed, and the comments still behave like before.

This is my first question on this site, I am not sure if this is a bug or not so I preffered not to open a github issue. Please let me know if there are improvements to be made for the way I asked my question.

Thank you for your help.

vim.opt.formatoptions:remove('r') does work for me. Could it be that you are modifying this option somewhere else in your configuration? maybe one of your plugins is modifying it?

I created a blank init.lua, and I put in it the following line :

I quit neovim, then enter neovim again with “nvim init.lua”. I insert a comment to test if the option worked, it didn’t.
Then, i got the idea to source the file using a neovim command, even though i tought quitting and entering again would do the same.
Inside neovim, i enter “:so %”. without quitting, i notice the option is now set, and the comment behaves like intended. I quit neovim and enter again, only to find out it went back to not working again. I can get it to work with “:so %” while neovim is open, but it seems like it gets reset everytime i close neovim.

I am using Neovim v0.8.3.
Could you let me know if you are able to reproduce this behavior ?

In the meantime i found a workaround, I put this line in my init.lua :
vim.cmd([[autocmd BufEnter * set formatoptions-=r]])
It does feel a bit dirty though…

Thank you for taking the time to answer my question, I really appreciate it. Have a good day :slight_smile:

I searched a bit further and realized that other people have had this issue as well: Can I disable continuation of comments to the next line in Vim? - Super User

In fact, the first comment below that answer suggests that to “permanently” solve the issue, you should add an autocmd, like you did (that comment has a bunch of likes).

But now, the thing I don’t understand is why I don’t have that issue if I haven’t added such autocmd. Hopefully, somebody with more knowledge can explain what is going on.

1 Like

This has been a long standing issue since old vim days, and to investigate the problem, use command :verb set formatoptions to see the last file that modifies formatoptions setting.

For the most case that I’m aware of is that vim has default file-type specific plugins for each files (.c, .cpp, .lua, etc.) and those file-type plugins are sourced after your init.lua is sourced, which means your formatoptions gets overriden by default plugins when you load a file to the buffer even though you specify your options in the init.lua file. The solution is as you’re doing right now to make an autocmd to set formatoptions when you enter a buffer, or according to file types.

Refer to this document: Starting - Neovim docs to learn more about the order of Neovim config files are sourced.

Thank you ! I’ll look into it.