Suppose, init.lua
contains the following:
vim.g.test_var1 = 0
vim.g.test_var2 = 0
vim.g.test_var3 = 0
local aug_test = vim.api.nvim_create_augroup('AuEnv', {clear=true})
-- test_var1 is incremented by 1 when switching between two windows
-- (or between a currently displayed buffer and a hidden one)
vim.api.nvim_create_autocmd('BufEnter,WinEnter', {
callback = function ()
-- print('test_var1', vim.g.test_var1)
vim.g.test_var1 = vim.g.test_var1 + 1
end,
group = aug_test,
})
-- test_var2 is incremented by 2 when switching between two windows.
vim.api.nvim_create_autocmd({'BufEnter', 'WinEnter'}, {
callback = function ()
-- print('test_var2', vim.g.test_var2)
vim.g.test_var2 = vim.g.test_var2 + 1
end,
group = aug_test,
})
-- test_var3 is incremented by 2 when switching between two buffers.
-- :e some_buf
-- :e another_buf
-- <C-6>
vim.api.nvim_create_autocmd({'BufEnter', 'BufWinEnter'}, {
callback = function ()
-- print('test_var3', vim.g.test_var3)
vim.g.test_var3 = vim.g.test_var3 + 1
end,
group = aug_test,
})
If we open at least two buffers (in split - testing var1 and var2, or ‘one active/one hidden’ - testing var2 and var3) and start switching from one window to another or from one buffer to another, and then type :echo test_var1 test_var2 test_var3
, they all will have different values, but what is interesting is that test_var1
and test_var2
do. Though, in the both cases, the same events are specified: for test_var1
, it is 'BufEnter,WinEnter'
, whereas, for test_var2
– {'BufEnter', 'WinEnter'}
. That is, specifying events as a table will lead to double execution of autocmd when two of the events are valid.
Is it intended behavior? If it is, how can I make it so that an autocmd executes just once if both events come