Search code examples
formatneovimftplugin

Neovim vim.opt:remove doesn't actually change the option


I came across some problem in my neovim settings, and something weird happens when I use vim.opt:remove to change the formatoptions.

First, my default formatoptions is jcroql, because I run :verbose set formatoptions? and it returns:

  formatoptions=jcroql
        Last set from /usr/share/nvim/runtime/ftplugin/lua.vim line 18

Then, I want to change it, removing ro options. According to this answer, I should use after-directories to change this option.

So, I create a file ~/.config/nvim/after/ftplugin/lua.lua to change this option for lua files. And here comes the question.

I wrote in this file:

vim.opt.formatoptions:remove('ro')
-- vim.cmd([[set fo-=ro]])

Using neovim lua api to change formatoptions, I found

  formatoptions=ojqlcr
        Last set from Lua

The order and modifier changes, but ro is still in formatoptions.

Using native way to change formatoptions, aka,

-- vim.opt.formatoptions:remove('ro')
vim.cmd([[set fo-=ro]])

I found:

  formatoptions=jcql
        Last set from Lua

And it works.


Why vim.opt.formatoptions:remove doesn't behave what is descriped in it's document? I expect vim.opt.formatoptions:remove('ro') is equal to set fo-=ro in vim script.

Additionally, is there a way to globally remove ro in all filetypes' formatoptions using after-directories?


Solution

  • Inspired by this answer, I tried to use separated remove calls like this:

    -- vim.opt.formatoptions:remove('ro')
    vim.opt.formatoptions:remove("r")
    vim.opt.formatoptions:remove("o")
    -- vim.cmd([[set fo-=ro]])
    

    And now this works! I found :verbose set formatoptions? to be:

      formatoptions=jclq
            Last set from Lua
    

    Although the default formatoptions is jcroql, including a ro in middle. I found that /usr/share/nvim/runtime/ftplugin/lua.vim line 18 is

    setlocal formatoptions-=t formatoptions+=croql
    

    Maybe it's just a coincidence to see jcroql in the formatoptions.