I would like to disable the colorcolumn in a buffer when switching to a different buffer. I try to perform this with a autocommand using the BufLeave Event.
In Neovim I use the following
local set_colorcolumn = function()
local buf = vim.api.nvim_get_current_buf()
vim.notify("BufEnter " .. tostring(buf))
vim.opt_local.colorcolumn = "80"
end
local unset_colorcolumn = function()
local buf = vim.api.nvim_get_current_buf()
vim.notify("BufLeave " .. tostring(buf))
vim.opt_local.colorcolumn = "0"
end
local colorcolumn_group = vim.api.nvim_create_augroup("CursorColumn", { clear = true })
vim.api.nvim_create_autocmd(
{ "BufEnter" },
{ pattern = "*.lua", group = colorcolumn_group, callback = set_colorcolumn }
)
vim.api.nvim_create_autocmd(
{ "BufLeave" },
{ pattern = "*.lua", group = colorcolumn_group, callback = unset_colorcolumn }
)
In Vim it would be
augroup colcol
au BufEnter *.lua :setlocal colorcolumn=80
au BufLeave *.lua :set colorcolumn=0
augroup end
In both cases the colorcolumn persists when switching buffers. I also tried to call redraw in the neovim case, with no success.
Can anybody explain why I'm experiencing this behavior? Is this to be expected.
++nested
?