0

I want a function to fire whenever the cursor moves. How can I do this?

1 Answer 1

3

To run anything automatically in Vim/Neovim after an event such as a file save(BufWrite) or your cursor moving(CursorMoved) we can use an autocommand.

Here is the Lua implementation for your specific case:

vim.cmd[[
  augroup cursor_moved
    autocmd!
    autocmd CursorMoved FileName source <afile> | execute 'lua foo(arg1,arg2)'
  augroup end
]]

For more information on autocommands read https://neovim.io/doc/user/autocmd.html

Be careful when using autocommands as they are very powerful and can easily break your editor.

1
  • 1
    Wrapping with vim.cmd is certainly an option, but since OP asked for a lua-based solution, you should use :h nvim_create_autocmd().
    – 3N4N
    Sep 20, 2022 at 9:16

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.