2

I started using neovim recently and for a learning Example i tried to create a basic plugin to update packages for node modules inside the package.json

The idea is when the text changes or when trying to undo i clear the buffer.

Calling this function : lua Clear_highlights() do the job but it's seems when the text changes or undo seems not triggering the function Clear_highlights() am for sure missing something if someone can help me catch this will be awesome.

Thank you.

line 20 : Github repo

-- Clear buffer
Clear_highlights = function()
    vim.api.nvim_buf_clear_namespace(0, require("nodePackageCheck").Config.get_namespace_id(), 0, -1)
end

vim.cmd([[autocmd! TextChanged,TextChangedI, BufWritePost  * lua Clear_highlights()]])

2 Answers 2

2
vim.cmd([[autocmd! TextChanged,TextChangedI, BufWritePost  * lua Clear_highlights()]])
  • autocmd! is for deleting handlers/groups, not defining them. Remove the bang (!).
  • Random whitespace confuses vim's parser: here it treats BufWritePost as the "pattern", and * as the start of the handler (command).

Try this instead:

vim.cmd([[autocmd TextChanged,TextChangedI,BufWritePost * lua Clear_highlights()]])
Sign up to request clarification or add additional context in comments.

1 Comment

It does what i want thanks for the explaining.
0

My final solution was a remapping undo in normal mode and call Clear_highlights() with.

vim.api.nvim_set_keymap("n", "u", ":undo<CR>:lua Clear_highlights()<CR>", { noremap = true, silent = true })

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.