wbthomason / packer.nvim

A use-package inspired plugin manager for Neovim. Uses native packages, supports Luarocks dependencies, written in Lua, allows for expressive config
MIT License
7.72k stars 262 forks source link

Lazy-loading a plugin triggers BufRead autocommands defined elsewhere #1106

Open xigoi opened 1 year ago

xigoi commented 1 year ago

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})