Skip to content

Instantly share code, notes, and snippets.

@MSMazaya
Last active January 27, 2023 21:35
Show Gist options
  • Save MSMazaya/ba9dc14e3f53bf1155ae84628e2445b9 to your computer and use it in GitHub Desktop.
Save MSMazaya/ba9dc14e3f53bf1155ae84628e2445b9 to your computer and use it in GitHub Desktop.
Neovide stop cursor animation when going to or after going from command line

I was really annoyed by this behaviour of neovide animation. neovide behavior It happened because I remap ctrl+s to :w to save a file, and generally I don't want the cursor to be animated when it goes to the command line. To solve this issue, I use a configuration as follows.

PreviousMode = vim.api.nvim_get_mode().mode

local function animateCursor()
    vim.g.neovide_cursor_animation_length = 0.06
    vim.g.neovide_cursor_trail_size = 0.7
end

local function stopCursorAnimation()
    vim.g.neovide_cursor_animation_length = 0
    vim.g.neovide_cursor_trail_size = 0
end

local function callWithDelay(fn, ms)
    local timer = vim.loop.new_timer()
    timer:start(ms, 0, vim.schedule_wrap(function()
        fn()
        timer:close()
    end))
end

vim.api.nvim_create_autocmd("ModeChanged", {
    group = vim.api.nvim_create_augroup("neovide_cursor_handler", { clear = true }),
    callback = function()
        mode = vim.api.nvim_get_mode().mode
        if mode == "c" then
            stopCursorAnimation()
        else
            if PreviousMode == "c" then
                stopCursorAnimation()
                callWithDelay(animateCursor, 1000)
            else
                stopCursorAnimation()
            end
        end
        PreviousMode = mode
    end,
})

It basically listen to the mode changes in neovim, if it changed to "c" (represents command line mode) then I stopped the cursor animation. But tricky part is to stop animating cursor after exiting the command line mode, the only implementation I could do is to first stop the animation and then animate back the cursor after a reasonable delay (in my case is 1000 ms).

Hopefully anyone may found this helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment