Don't across that column in Neovim
Deadcolumn is a neovim plugin to assist users in maintaining a specific column
width in their code. This plugin operates by gradually displaying the
colorcolumn
as the user approaches it. It is useful for people who wish to
keep their code aligned within a specific column range.
Table of Contents
-
Gradually display the
colorcolumn
as the user approaches it: -
Display the column in warning color if current line exceeds
colorcolumn
: -
Handle multiple values of
colorcolumn
properly::set colorcolumn=-10,25,+2 textwidth=20
:
-
Show the colored column only when you need it
- Using lazy.nvim
lua require('lazy').setup({
{ 'Bekaboo/deadcolumn.nvim' }
})
- Using packer.nvim
require('packer').startup(function(use)
use 'Bekaboo/deadcolumn.nvim'
end)
You don't need to call the setup()
function if you don't want to change the
default options, the plugin should work out of the box if you set colorcolumn
to a value greater than 0.
The following is the default options, you can pass a table to the setup()
function to override the default options.
local opts = {
scope = 'line',
modes = { 'i', 'ic', 'ix', 'R', 'Rc', 'Rx', 'Rv', 'Rvc', 'Rvx' },
blending = {
threshold = 0.75,
colorcode = '#000000',
hlgroup = {
'Normal',
'background',
},
},
warning = {
alpha = 0.4,
offset = 0,
colorcode = '#FF0000',
hlgroup = {
'Error',
'background',
},
},
extra = {
follow_tw = nil,
},
}
require('deadcolumn').setup(opts) -- Call the setup function
-
scope
(string): The scope for showing the colored column, there are several possible values:-
'line'
: colored column will be shown based on the length of the current line. -
'buffer'
: colored column will be shown based on the length of the longest line in current buffer (up to 1000 lines around current line). -
'visible'
: colored column will be shown based on the length of the longest line in the visible area. -
'cursor'
: colored column will be shown based on current cursor position.
-
-
modes
(table): In which modes to show the colored column. -
blending
(table): Blending options.-
threshold
(number): The threshold for showing the colored column.-
If
threshold
is a number between 0 and 1, it will be treated as a relative threshold, the colored column will be shown when the current line is longer thanthreshold
times thecolorcolumn
. -
If
threshold
is a number greater than 1, it will be treated as a fixed threshold, the colored column will be shown when the current line is longer thanthreshold
characters.
-
-
colorcode
(string): The color code to be used as the background color for blending. -
hlgroup
(table): The highlight group to be used as the background color for blending.- If the highlight group is not found,
colorcode
will be used.
- If the highlight group is not found,
-
-
warning
(table): Warning color options.-
alpha
(number): The alpha value for the warning color, blended with the background color. -
offset
(number): The offset for the warning color, the warning color will be shown when the length of the line exceedscolorcolumn
byoffset
characters. -
colorcode
(string): The color code for the warning color. -
hlgroup
(table): The highlight group for the warning color.- If the highlight group is not found,
colorcode
will be used.
- If the highlight group is not found,
-
-
extra
(table): Extra functionalities.-
follow_tw
(nil|string):-
If
follow_tw
isnil
: the functionalities is disabled. -
If
follow_tw
is string:colorcolumn
will be set to this value whentextwidth
is set, and will be restored to the original value whentextwidth
is unset.Suggested value for this option is
'+1'
.
-
-
If you are using the default config, this is expected.
The default config makes colorcolumn visible only in insert mode and replace
mode, so it clears cc
in normal mode and reset it to the original value when
you enter insert mode or replace mode. As long as the colorcolumn is displayed
correctly in insert mode and replace mode, you don't need to worry about this.
If you want to see colorcolumn in normal mode, you can change the modes
option, see Options.
This can have several reasons:
-
If you are using the default config, it is expected that you can't see the colored column in normal mode, because the colored column is only shown in insert mode and replace mode by default. You can change the
modes
option to show the colored column in normal mode. -
Please make sure you have set
colorcolumn
to a value greater than 0 in your config. Notice that the output of:echo &cc
orlua =vim.wo.cc
may be empty even if you have setcolorcolumn
to a value greater than 0. This is because this plugin clearscolorcolumn
when it is not needed to conceal the colored column, see point 1. -
If you set
colorcolumn
to a relative value (e.g.'-10'
), make suretextwidth
is set to a value greater than 0.
This plugin does not set colorcolumn
for you, it only reads and uses the
value of colorcolumn
of the current buffer to show the colored column
when needed.
It leaves to you to set colorcolumn
for different filetypes, under different
conditions, which is more flexible compared to setting colorcolumn
in the
plugin setup function.
There are mainly two ways to set colorcolumn
for different filetypes:
-
Using
autocmd
:You can use the
autocmd
command to setcolorcolumn
for different filetypes.For example, you can set
colorcolumn
to 80 for markdown files:autocmd FileType markdown setlocal colorcolumn=80
-
Using
ftplugin
:You can also use the
ftplugin
directory to setcolorcolumn
for different filetypes.For example, you can create a file named
markdown.vim
in theftplugin
directory under your config directory, and setcolorcolumn
to 80 formarkdown
files:setlocal colorcolumn=80
If you are using a transparent background, the colored column may not be
displayed properly, since the background color of the colored column
dynamically changed based on the blending of 'Normal'
background color and
the orignial 'ColorColumn'
background color.
If Deadcolumn cannot find the 'Normal'
background color, it will use
'#000000'
(pure black) as the default background color for blending.
There is no way to fix this, since terminal emulators do not support setting a transparent background color for a specific character.
Workarounds:
-
You can set
opts.threshold
to 1 to disable blending when the length is smaller thancolorcolumn
and show the colored column only when it is greater thancolorcolumn
, OR -
You can assign a different highlight group or a fixed colorcode to be used for blending with the original
'ColorColumn'
background color, for example:require('deadcolumn').setup({ blending = { colorcode = '#1F2430', hlgroup = { 'NonText', 'background', }, }, })