packer.nvim icon indicating copy to clipboard operation
packer.nvim copied to clipboard

Lazy-loading a plugin triggers BufRead autocommands defined elsewhere

Open xigoi opened this issue 1 year ago • 0 comments

  • nvim --version: 0.8.0

Steps to reproduce

Put the following in your init.lua:

vim.api.nvim_create_autocmd("BufReadPost", {callback = function() print("Triggered") end})

Also configure a plugin to be lazy-loaded on a given command or key.

Restart Neovim and trigger the command/key to load the plugin.

Actual behaviour

The autocommand will be triggered by the loading of the plugin.

Expected behaviour

No external autocommand will be triggered by the loading of the plugin.

Workaround

Put a condition in the autocommand to make it trigger only once per buffer:

vim.api.nvim_create_autocmd("BufReadPost", {callback = function()
  if not vim.b.my_autocmd_triggered then
    print("Triggered")
    vim.b.my_autocmd_triggered = true
  end
end})

xigoi avatar Oct 23 '22 19:10 xigoi