0

I'm writing a plugin for NeoVim in Lua and I'm trying to keep the state of my plugin in the vim.b table:

vim.b.myplugin = vim.b.myplugin or {
    "property" = "value",
    ...
}

But it turns out, that I can't change any properties later on. All values remain the same.

Here is a oneliner showing the problem:

:lua vim.b.myplugin = {p = "a"} ; print(vim.b.myplugin.p) ; vim.b.myplugin.p = "b" ; print(vim.b.myplugin.p)

prints

a
a

What am I doing wrong and how can I store my buffer-specific state in a lua plugin?

1

1 Answer 1

1

It seems that neovim creates a new table each time a b type variable is accessed ;

:lua vim.b.myplugin = {p = "a"}; print(vim.b.myplugin); print(vim.b.myplugin);

table: 0x7f09e16bcb28                                                                                           
table: 0x7f09e15aca80

Any modifications to the table are not propagated back to the buffer variable; for instance this also does not work;

lua vim.b.myplugin = {p = "a"};
x = vim.b.myplugin; print(vim.inspect(x));
x.p = "b"; print(vim.inspect(vim.b.myplugin))

One workaround would be to replace the table wholly

val = vim.b.myplugin
val.p = "b"
vim.b.myplugin = val    

Alternatively you can use a global variable indexed by buffer

bufg = {}
bufg[vim.fn.bufnr()] = {p = "a"}

But of course, this never makes it to a real buffer variable in b:.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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