luaneovimneovim-plugin

How to run a function everytime I enter Insert Mode in Nvim?


i am making a small plugin for myself and i just want to run a simple command whenever I enter Insert mode.

Currently i have tried the following

vim.api.nvim_create_autocmd({"InsertEnter"}, {
    pattern = "*",
    callback = function()
--        if vim.b[args.buf].is_pandora then return end
        print("Insert mode in normal buffer!")
        vim.notify("Insert mode in normal buffer!")
    end,
})

I was expecting to see this message whenever i enter insert mode.

this sits in nvim/lua/plugin-name/init.lua

and is called by the main init.lua file by using require("plugin-name")

Further there are no errors to go by and I have tried reading the documentations as well but cant seem to find any examples .

also this is my first time working with lua extensively or making a nvim plugin so I might have missed something obvious

vim.api.nvim_create_autocmd("InsertLeave", {
  callback = function()
      print("Exited insert mode!");
  end
});

this works for some reason but not when i just change InsertLeave to InsertEnter


Solution

  • I can't reproduce what you're describing. If I put the following in ~/config/nvim/lua/simple/init.lua and then require("simple"), then I get the expected messages both when I enter and leave insert mode.

    vim.api.nvim_create_autocmd("InsertLeave", {  
        callback = function()  
            print("Exited insert mode!")  
        end,  
    })  
      
    vim.api.nvim_create_autocmd("InsertEnter", {  
        callback = function()  
            print("Entered insert mode!")  
        end,  
    })
    

    Have you tried running :messages to check (all the) recent messages?

    To answer your follow up question about why you only see one message, it has to do with the cmdheight option. By default, that is set to 1. When you enter insert, by default the command line shows -- INSERT --. That message goes first into the command line and so you don't see the "Entered insert mode!" message. It's there, but not visible. When you exit insert mode, nothing new is sent to the messages about the mode change, so the "Exited insert mode!" message is immediately visible. (For the record, none of these messages actually "appears in a buffer." The messages appear in the command line.)

    To experiment and see what I mean, put the following into a file called init.lua.

    vim.api.nvim_create_autocmd("InsertLeave", {  
        callback = function()  
            print("Exited insert mode!")  
        end,  
    })  
      
    vim.api.nvim_create_autocmd("InsertEnter", {  
        callback = function()  
            print("Entered insert mode!")  
        end,  
    })
    vim.o.cmdheight=2
    

    Start Neovim with nvim --clean -u init.lua and enter insert mode. You should see the message immediately because now you display two lines in the command line for messages. (That's what the vim.o.cmdheight = 2 line does.)

    In any case, to answer your original question, there was nothing wrong with your autocommand.