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.

7
  • 2
    Works for me in vim asciinema.org/a/ESUgdzhHiPKQevlSG534rvOyH
    – Maxim Kim
    Commented Jan 25 at 10:12
  • Pls clarify if it only doesn't work in neovim and you need it there.
    – Maxim Kim
    Commented Jan 25 at 10:14
  • How are you switching buffers? Could it be a missing ++nested? Commented Jan 25 at 13:08
  • I have tested on Neovim and it seems to work fine. Commented 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. Commented Jan 26 at 13:51

2 Answers 2

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. Commented Feb 4 at 12:32
0

If you want the colorcolumn to follow the cursor, you can do the following:

augroup colcol
  au!
  au VimEnter,WinEnter,BufWinEnter *.lua :setlocal colorcolumn=80
  au WinLeave *.lua :set colorcolumn=0
augroup end

I prefer to write autocommands for Neovim using vim.cmd, then it would look like:

vim.cmd([[
  augroup colcol
    au!
    au VimEnter,WinEnter,BufWinEnter *.lua :setlocal colorcolumn=80
    au WinLeave *.lua :set colorcolumn=0
  augroup end
]])
2
  • Thanks, this looks promising. I think I understand now what flaw I had with my solution. Commented Oct 23 at 18:40
  • I had these events to enable cursorline for the window that was active, but I can't remember if I figured out that set of autocommand events myself or picked it up from someplace else -- probably latter, so you might find a reference to it somewhere. Commented Oct 24 at 6:18

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.