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

2

2

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?

Hwahwan answered 16/5, 2023 at 3:0 Comment(0)
H
1

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.

Hwahwan answered 16/5, 2023 at 14:28 Comment(1)
I tried this exact thing, but it's not working for me. ftplugin/lua.vim is still updating the formatoptions last!Litharge
S
0

Using after directory to remove ro in formatoptions seems impossible, as you have to create a ftplugin for every filetype you use. A workaround for it is using autocmd:

vim.api.nvim_create_autocmd({ "FileType" }, {
  group = vim.api.nvim_create_augroup("FormatOptions", { clear = true }),
  pattern = { "*" },
  callback = function()
    vim.opt_local.fo:remove("o")
    vim.opt_local.fo:remove("r")
  end,
})

It works for me.

Sevier answered 19/5, 2024 at 5:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.