1

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.

Actual Behavior

Expected Behavior

Can anybody explain why I'm experiencing this behavior? Is this to be expected.

6
  • 2
    Works for me in vim asciinema.org/a/ESUgdzhHiPKQevlSG534rvOyH
    – Maxim Kim
    Jan 25 at 10:12
  • Pls clarify if it only doesn't work in neovim and you need it there.
    – Maxim Kim
    Jan 25 at 10:14
  • How are you switching buffers? Could it be a missing ++nested? Jan 25 at 13:08
  • I have tested on Neovim and it seems to work fine. Jan 25 at 14:31
  • 1
    It works in thew way shown in the asciinema shown by Maxim Kim, but it does not work if you take the steps that I've described above. Even in the asciinema it does not work as expected when switching the first time to the split buffer. The colorcolumn persists the first time, when creating the split. 1. open a lua file 2. open a help page e.g. :h BufLeave Then for that time it does not work. But then when you switch between the two open buffers it works again. Jan 26 at 13:51

1 Answer 1

0

I believe there is a miss understanding about the pattern. The code only make the modification when you switch to a lua file.

I would try:

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 = "*", group = colorcolumn_group, callback = set_colorcolumn }
)

vim.api.nvim_create_autocmd(
    { "BufLeave" },
    { pattern = "*", group = colorcolumn_group, callback = unset_colorcolumn }
)
1
  • 1
    This doesn't represent my usecase. I want a specific colorcolumn per filetype and by default no colorcolumn. Your proposal will enable colorcolumn for any filetype including e.g. vimdoc. But I would want a defined colorcolumn for specific filea like lua, but only if the cursor is in the buffer. Feb 4 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.