fix: Make colorcolumn work properly

Setting `colorcolumn` as a window option breaks any window
that does not need it set.
Use an autocmd to fix this.
This commit is contained in:
Robert Morrison 2026-04-09 20:25:43 +01:00
parent 3b4e204195
commit 0d623139fa
4 changed files with 33 additions and 4 deletions

View File

@ -1,3 +1,2 @@
vim.wo.spell = true
vim.bo.textwidth = 75
vim.wo.colorcolumn = "75,80"

2
ftplugin/tex.lua Normal file
View File

@ -0,0 +1,2 @@
vim.wo.spell = true
vim.bo.textwidth = 80

View File

@ -1,6 +1,5 @@
vim.wo.spell = true
vim.bo.textwidth = 75
vim.wo.colorcolumn = "75,80"
vim.api.nvim_buf_create_user_command(0, "OpenPdf", function()
local filepath = vim.api.nvim_buf_get_name(0)

View File

@ -1,7 +1,8 @@
-- Control cursorline option
-- This function allows the easy creation of autocommands that configure cursorline for one buffer
-- either based on events or filetypes
local auGroupCursorLine = vim.api.nvim_create_augroup("CursorLine", { clear = true })
local auGroupCursorLine = vim.api.nvim_create_augroup("CursorLine",
{ clear = true })
local set_cursorline = function(event, value, pattern)
vim.api.nvim_create_autocmd(event, {
group = auGroupCursorLine,
@ -18,7 +19,8 @@ set_cursorline("FileType", false, "TelescopePrompt")
-- Wrangle formatoptions
-- Format options is local to the buffer
-- This autocommand fixes formatoptions for all buffers at load
local auGroupFormatOptions = vim.api.nvim_create_augroup("FormatOptions", { clear = true })
local auGroupFormatOptions = vim.api.nvim_create_augroup("FormatOptions",
{ clear = true })
vim.api.nvim_create_autocmd("FileType", {
group = auGroupFormatOptions,
pattern = "*",
@ -28,3 +30,30 @@ vim.api.nvim_create_autocmd("FileType", {
- "o" -- and for newlines
end,
})
-- Do some magic for colorcolumn
-- if set as a window option for a file, anything that uses that window
-- at a later date will inherit that even if it doesn't make sense.
local auGroupColorColumn = vim.api.nvim_create_augroup("ColorColumn",
{clear=true})
vim.api.nvim_create_autocmd( 'BufEnter', {
group = auGroupColorColumn,
pattern = "*",
callback = function (_)
-- if we set textwidth we show a colorcolumn
if vim.bo.textwidth > 0 then
local warn=vim.bo.textwidth - 5
vim.wo.colorcolumn=string.format("%d,%d", warn, vim.bo.textwidth)
else
vim.wo.colorcolumn=""
end
end,
}
)
vim.api.nvim_create_autocmd('BufLeave',{
group=auGroupColorColumn,
pattern = '*',
callback = function ()
vim.wo.colorcolumn=""
end
})