A rewrite of smartcolumn.nvim with performance improvements, saner defaults, and more features.
It is a plugin to automatically disable the colour column when it is not needed.
Below is a video demonstration of the plugin in action.
demo.mp4
- Neovim v0.10+
{
"hankertrix/nerd_column.nvim",
event = "BufEnter",
opts = {}
}vim.pack.add({ "https://github.com/hankertrix/nerd_column.nvim" }){
"hankertrix/nerd_column.nvim",
config = function() require("nerd_column").setup() end
}use("hankertrix/nerd_column.nvim")Plug("hankertrix/nerd_column.nvim")Set up the plugin in your init.lua. This is not needed with lazy.nvim and
pckr.nvim if following the installation instructions above.
require("nerd_column").setup()The colour_column can be an integer or a list of integers.
For example:
---@type integer|integer[]
colour_column = 80
colour_column = { 80, 120 }The custom_colour_column can either be a table of file types mapped to an
integer or a list of integers. It can also be a function that takes an integer
representing the buffer ID, an integer representing the window ID, and a string
representing the file type, and returns an integer or a list of integers.
For example:
---@type integer|integer[]
custom_colour_column = {
lua = { 80, 120 },
markdown = 100,
}
---@type fun(
--- buffer: integer,
--- window: integer,
--- file_type: string,
---): integer|integer[]
custom_colour_column = function(buffer, window, file_type)
return 120
endThe scope option refers to the scope within which to check whether the lines
have exceeded the colour column configuration. It can be either file,
window, or line.
filerefers to the entire file.windowrefers to the visible part of the current window.linerefers to the current line.
For example:
---@type "file"|"window"|"line"
scope = "window"The respect_editor_config option tells the plugin whether it should respect
the max_line_length configuration in the .editorconfig file. It can be
either true or false.
For example:
---@type boolean
respect_editor_config = trueNote that this option overrides the custom_colour_column configuration.
The enabled option enables the plugin.
For example:
---@type boolean
enabled = trueThe always_show option sets whether the plugin should always show the colour
column.
However, your disabled_file_types configuration will be respected when
always_show is set to true, which means the colour column will not show on
buffers with file types inside the disabled_file_types list when always_show
is set to true.
For example:
---@type boolean
always_show = trueThe maximum_line_count option sets the maximum number of lines the plugin will
check. When a file has more lines than the maximum_line_count, the plugin will
automatically change the scope to window so that Neovim doesn't become
unbearably slow and laggy to use.
For example:
---@type integer
maximum_line_count = 40000The transform_colour_column option is a function that transforms the colour
column before it is actually displayed, allowing you to trigger the colour
column display on a certain line length, but display the colour column at a
different column from the colour column that triggered the display of the colour
column.
For example, if you would like to have the colour column be displayed when the
line length is past 80, but you want the colour column to show up at column
81, you can use the function below to achieve that:
---@type fun(colour_column: integer|integer[]): integer|integer[]
transform_colour_column = function(colour_column)
return colour_column + 1
endThe transform_colour_column function receives the colour column as an argument
which is either an integer or a list of integers, and returns the modified
colour column, which is either an integer or a list of integers.
If you have multiple colour columns in your configuration, i.e. a list of colour
columns, make sure you handle them in the transform_colour_column function,
like shown below, or you will break the plugin.
---@type fun(colour_column: integer|integer[]): integer|integer[]
transform_colour_column = function(colour_column)
if type(colour_column) == "table" then
return vim.tbl_map(function(item) return item + 1 end, colour_column)
end
return colour_column + 1
endThe disabled_file_types option is a list of file types in which the plugin
should be disabled. It is a list of strings. You can use * at the end of a
file type to match a file type prefix, like Neogit*.
For example:
---@type string[]
disabled_file_types = { "help", "checkhealth", "netrw", "qf" }The default configuration for the plugin is as follows:
---@type NerdColumn.Config
local default_config = {
colour_column = 80,
custom_colour_column = {},
scope = "file",
respect_editor_config = true,
enabled = true,
always_show = false,
maximum_line_count = 40000,
transform_colour_column = function(colour_column) return colour_column end,
disabled_file_types = {
"help",
"checkhealth",
"netrw",
"qf",
"man",
-- Plugin specific file types
"packer",
"dirvish",
"dirbuf",
"diff",
"mason",
"lazy",
"lspinfo",
"null-ls-info",
"fugitive",
"undotree",
"aerial",
"harpoon",
"minifiles",
"trouble",
"spectre_panel",
"noice",
"fish",
"zsh",
"typr",
"typrstats",
"Trouble",
"NvimTree",
"WhichKey",
"Telescope*",
"Neogit*",
"snacks_*",
},
}You don't have to pass the configuration above to the setup function if you
want to use the default configuration.
If you would like to make use of the default configuration options in your own
configuration, like the disabled_file_types for example, you can use the
following snippet to access it.
require("nerd_column").default_configYou can control the state of the plugin via the use of the global option
vim.g.nerd_column_enabled and the buffer-local option
vim.b.nerd_column_enabled. If you have set the plugin to be enabled by default
by setting enabled to true in your configuration, the global option
vim.g.nerd_column_enabled will be automatically set to true when the plugin
is set up.
The value of the buffer-local option vim.b.nerd_column_enabled is checked
first, and if it is not set, i.e. vim.b.nerd_column_enabled is nil, then the
value of the global option vim.g.nerd_column_enabled will be used instead.
This way, you can disable the plugin in specific buffers by setting
vim.b.nerd_column_enabled to false.
Nerd Column provides 4 commands:
-
NerdColumnEnableto enable the plugin. It can also be accessed from Lua by using:require("nerd_column").enable()
This command will set both
vim.g.nerd_column_enabledandvim.b.nerd_column_enabledtotrue, as most users would expect the command to work globally.You can also pass
trueto the function to enable the plugin in all buffers. -
NerdColumnDisableto disable the plugin. It can also be accessed from Lua by using:require("nerd_column").disable()
This command will set both
vim.g.nerd_column_enabledandvim.b.nerd_column_enabledtofalse, as most users would expect the command to work globally.You can also pass
trueto the function to enable the plugin in all buffers. -
NerdColumnToggleto toggle the plugin. It can also be accessed from Lua by using:require("nerd_column").toggle()
This command will get the current state of the plugin from
vim.b.nerd_column_enabledfirst. Ifvim.b.nerd_column_enabledis not set, i.e.vim.b.nerd_column_enabledisnil, then it will take the value ofvim.g.nerd_column_enabled. Then, the command will toggle the plugin state and set bothvim.g.nerd_column_enabledandvim.b.nerd_column_enabledto the toggled state. The reason for this is that most users would expect the command to work globally, and that the toggle should act on the current state of the plugin in the current buffer.You can also pass
trueto the function to enable the plugin in all buffers. -
NerdColumnRevealto reveal the first location in the scope that exceeded the minimum colour column by moving the cursor to that location.It can also be accessed from Lua by using:
require("nerd_column").reveal()
The plugin saves the first location that exceeds the colour column into
vim.b.nerd_column_exceeded_position. It is set tonilif the colour column has not been exceeded or the plugin is disabled. Otherwise, you can get the line number fromvim.b.nerd_column_exceeded_position.lineand the column number fromvim.b.nerd_column_exceeded_position.column.
This plugin is licenced under the GNU AGPL v3 licence.