The fastest Neovim colorizer
| Files | Last commit | Last update |
|---|---|---|
| 8 months ago | ||
| 3 months ago | ||
| 3 months ago | ||
| 1 year ago | ||
| 3 months ago | ||
| 7 months ago | ||
| 3 months ago | ||
| 1 year ago | ||
| 7 months ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 6 years ago | ||
| 7 months ago | ||
| 3 months ago | ||
| 7 months ago | ||
| 8 months ago |
colorizer.lua
一款高性能的 Neovim 颜色高亮插件,无任何外部依赖。采用高性能 Luajit 编写。

截图测试 — 针对每个解析器和显示模式的 CI 生成视觉测试。如果发现显示异常,请点击测试旁的 [N] 链接报告问题。
为什么选择 colorizer.lua?
- 速度快:手写的基于前缀树的解析器,采用字节级调度。仅处理可见行。
- 零依赖:只要系统支持
malloc()和free(),即可运行(Linux、macOS、Windows)。 - 格式支持广泛:十六进制(
#RGB、#RRGGBB、#RRGGBBAA、#AARRGGBBQML、0xAARRGGBB)、CSS 函数(rgb()、hsl()、hwb()、lab()、lch()、oklch()、color())、CSS 自定义属性(var(--name))、命名颜色、xterm/ANSI 256 色、Tailwind CSS、Sass 变量以及自定义解析器 —— 支持任何文件类型。 - 多种显示模式:背景色(自动对比度文本)、前景色、下划线(通过
sp设置颜色)和虚拟文本(行内或行尾)。 - 优先级高于 treesitter:使用
vim.hl.priorities(诊断/用户),因此 colorizer 高亮始终优先于 treesitter 语法颜色。
安装
要求 Neovim 版本 >= 0.10.0 且已设置 set termguicolors
-- vim.pack
vim.pack.add({
"https://github.com/catgoose/nvim-colorizer.lua"
})
require("colorizer").setup()
-- lazy.nvim
{
"catgoose/nvim-colorizer.lua",
event = "BufReadPre",
opts = {},
}
示例
-- Enable all CSS color formats
require("colorizer").setup({
options = { parsers = { css = true } },
})
-- CSS functions only, with virtualtext display
require("colorizer").setup({
options = {
parsers = { css_fn = true },
display = {
mode = "virtualtext",
virtualtext = { position = "after" },
},
},
})
-- Preset with individual override
require("colorizer").setup({
options = {
parsers = { css = true, rgb = { enable = false } },
},
})
-- Per-filetype overrides
require("colorizer").setup({
options = {
-- base options applied to every filetype unless overridden below
parsers = {
hex = { default = false },
names = { enable = false },
tailwind = { enable = false },
},
},
filetypes = {
"*",
"!markdown", -- exclude markdown entirely
html = { mode = "foreground" }, -- legacy flat key
cmp_docs = { always_update = true }, -- legacy flat key
-- Each entry can also use the same nested format as `options`,
-- deep-merged on top of the base options:
css = {
parsers = {
hex = { default = true },
oklch = { enable = true },
},
},
javascriptreact = {
parsers = { tailwind = { enable = true, lsp = true } },
},
},
buftypes = {
-- Same override format works for buftypes
terminal = { parsers = { hex = { default = true } } },
},
})
-- Enabling a parser per-filetype: both shapes below are valid
require("colorizer").setup({
options = {
parsers = { names = { enable = false } }, -- disabled by default
},
filetypes = {
html = { names = true }, -- legacy flat shorthand
css = { parsers = { names = { enable = true, lowercase = false } } }, -- nested new-format
},
})
filetypes/buftypes中的每个值可接受嵌套格式(parsers = {...}、display = {...}、hooks = {...}、always_update)或 旧版扁平键(mode、names、RGB、rgb_fn等)。这两种形式都会被转换并深度合并到底层options中,因此你只需指定该文件类型需要变更的部分即可。尽管顶层推荐使用
options = { ... }形式,但filetypes/buftypes覆盖配置特意保留了这种混合格式兼容性——这能让简短的按文件类型调整保持简洁。总结如下
- 扁平的旧版解析器键(
names、RGB、rgb_fn等)直接放在文件类型表中,例如html = { names = true }- 结构化的解析器配置放在
parsers下,例如html = { parsers = { names = { enable = true } } }- 要调整子选项(
lowercase、camelcase等),请使用嵌套形式——扁平简写仅切换enable
解析器选项
十六进制 default 键
parsers.hex 中的 default 键为所有格式键(rgb、rgba、rrggbb、rrggbbaa、aarrggbb)设置默认值。未显式设置的任何格式键都将从 default 继承。显式设置的键始终优先。
-- Enable all hex formats
hex = { default = true }
-- Enable all hex formats except 8-digit (#RRGGBBAA)
hex = { default = true, rrggbbaa = false }
-- Disable all hex formats
hex = { default = false }
-- Only enable 6-digit hex
hex = { rrggbb = true }
-- Equivalent to the above (default is already false)
hex = { default = false, rrggbb = true }
注意: 其他解析器(
names、tailwind、sass)将enable用作简单的开关。default键是hex解析器独有的,因为它是唯一一个包含多个布尔格式子键的解析器。
自定义命名颜色
parsers.names.custom 会在内置 CSS 名称的基础上添加用户定义的名称到十六进制值的映射。传入一个表格,或者一个返回表格的函数以实现动态加载。启用 xcolor 可让相同的自定义名称在 LaTeX 的 name!percent 表达式中生效(参见 #211)。
require("colorizer").setup({
options = {
parsers = {
names = {
enable = true,
custom = {
thesisblue = "#1f4e79",
accentgreen = "#2e7d32",
},
},
xcolor = { enable = true }, -- LaTeX: thesisblue!30
},
},
})
默认配置
require("colorizer").setup({
filetypes = { "*" }, -- filetypes to highlight, "*" for all
buftypes = {}, -- buftypes to highlight
user_commands = true, -- enable user commands (ColorizerToggle, etc.)
lazy_load = false, -- lazily schedule buffer highlighting
options = {
parsers = {
css = false, -- preset: enables names, hex, rgb, hsl, oklch, css_var
css_fn = false, -- preset: enables rgb, hsl, oklch
names = {
enable = true, -- enable named colors (e.g. "Blue")
lowercase = true, -- match lowercase names
camelcase = true, -- match CamelCase names (e.g. "LightBlue")
uppercase = false, -- match UPPERCASE names
strip_digits = false, -- ignore names with trailing digits (e.g. "blue3")
custom = false, -- custom name-to-hex mappings; table|function|false
extra_word_chars = "-", -- extra chars treated as part of color name
},
hex = {
default = true, -- default value for unset format keys (see above)
rgb = true, -- #RGB (3-digit)
rgba = true, -- #RGBA (4-digit)
rrggbb = true, -- #RRGGBB (6-digit)
rrggbbaa = false, -- #RRGGBBAA (8-digit)
hash_aarrggbb = false, -- #AARRGGBB (QML-style, alpha first)
aarrggbb = false, -- 0xAARRGGBB
no_hash = false, -- hex without '#' at word boundaries
},
rgb = { enable = false }, -- rgb()/rgba() functions
hsl = { enable = false }, -- hsl()/hsla() functions
oklch = { enable = false }, -- oklch() function
hwb = { enable = false }, -- hwb() function (CSS Color Level 4)
lab = { enable = false }, -- lab() function (CIE Lab)
lch = { enable = false }, -- lch() function (CIE LCH)
css_color = { enable = false }, -- color() function (srgb, display-p3, a98-rgb, etc.)
tailwind = {
enable = false, -- parse Tailwind color names
update_names = false, -- feed LSP colors back into name parser (requires both enable + lsp.enable)
lsp = { -- accepts boolean, true is shortcut for { enable = true, disable_document_color = true }
enable = false, -- use Tailwind LSP documentColor
disable_document_color = true, -- auto-disable vim.lsp.document_color on attach
},
},
sass = {
enable = false, -- parse Sass color variables
parsers = { css = true }, -- parsers for resolving variable values
variable_pattern = "^%$([%w_-]+)", -- Lua pattern for variable names
},
xterm = { enable = false }, -- xterm 256-color codes (#xNN, \e[38;5;NNNm)
ls_colors = { enable = false }, -- LS_COLORS/SGR snippets (e.g. =38;5;196, =48;2;0;0;255)
xcolor = { enable = false }, -- LaTeX xcolor expressions (e.g. red!30)
hsluv = { enable = false }, -- hsluv()/hsluvu() functions
css_var_rgb = { enable = false }, -- CSS vars with R,G,B (e.g. --color: 240,198,198)
css_var = {
enable = false, -- resolve var(--name) references to their defined color
parsers = { css = true }, -- parsers for resolving variable values
},
custom = {}, -- list of custom parser definitions
},
display = {
mode = "background", -- string or list: "background"|"foreground"|"underline"|"virtualtext"
background = {
bright_fg = "#000000", -- text color on bright backgrounds
dark_fg = "#ffffff", -- text color on dark backgrounds
},
virtualtext = {
char = "■", -- character used for virtualtext
position = "eol", -- "eol"|"before"|"after"
hl_mode = "foreground", -- "background"|"foreground"
},
priority = {
default = 150, -- extmark priority for normal highlights
lsp = 200, -- extmark priority for LSP/Tailwind highlights
},
disable_document_color = true, -- true (all LSPs) | false | { lsp_name = true, ... }
},
hooks = {
should_highlight_line = false, -- function(line, bufnr, line_num) -> bool
should_highlight_color = false, -- function(rgb_hex, parser_name, ctx) -> bool
transform_color = false, -- function(rgb_hex, ctx) -> string
on_attach = false, -- function(bufnr, opts)
on_detach = false, -- function(bufnr)
},
always_update = false, -- update highlights even in unfocused buffers
debounce_ms = 0, -- debounce highlight updates (ms); 0 = no debounce
},
})
Tailwind CSS
Tailwind 颜色可通过捆绑的颜色数据(enable)或 Tailwind LSP 的 textDocument/documentColor(lsp)进行解析。两者可同时使用。
| 选项 | 行为 |
|---|---|
enable = true |
解析标准 Tailwind 颜色名称 |
lsp = true |
使用 Tailwind LSP 文档颜色 |
两者均为 true |
合并两种来源 |
lsp 接受布尔值简写或用于精细控制的表格:
require("colorizer").setup({
options = {
parsers = {
tailwind = { enable = true, lsp = true },
},
},
})
require("colorizer").setup({
options = {
parsers = {
tailwind = {
enable = true,
lsp = {
enable = true,
disable_document_color = true, -- default
},
update_names = true,
},
},
},
})
当启用 lsp.update_names = true 且同时激活 enable 和 lsp.enable 时,LSP 结果会反馈到名称解析器的颜色表中。基于名称的解析是即时的(可在 cmp 窗口、新缓冲区等场景中工作),但使用的是捆绑的颜色数据。LSP 速度较慢(需要服务器响应),但能从 tailwind.config.{js,ts} 中读取自定义颜色。通过将两者结合,缓冲区会立即使用基于名称的匹配进行着色,然后 LSP 结果会修正颜色并更新名称表,以便后续基于名称的高亮使用准确的值。

Neovim 内置 LSP 文档颜色(0.12+)
Neovim 0.12+ 通过 vim.lsp.document_color 提供内置的 textDocument/documentColor 支持,该功能在 LspAttach 时默认启用。这可能会导致重复高亮 —— 例如,即使 colorizer 设置为 virtualtext 模式,十六进制代码上仍会出现背景高亮。
Colorizer 会通过 display.disable_document_color(默认值为 true)在缓冲区附加时自动禁用 vim.lsp.document_color。这适用于所有 LSP 服务器,而不仅仅是 Tailwind。无需手动设置 LspAttach 自动命令。
disable_document_color 接受三种形式:
| 值 | 行为 |
|---|---|
true |
禁用所有 LSP 服务器的文档颜色(默认值) |
false |
保持 vim.lsp.document_color 激活 |
{ lsp_name = true, ... } |
仅为列出的服务器禁用 |
此外,当 tailwind.lsp 激活时,Tailwind 专用的 tailwind.lsp.disable_document_color(默认值也为 true)会处理 Tailwind LSP 在 colorizer 之后附加的情况。
要保持内置功能与 colorizer 同时激活:
require("colorizer").setup({
options = {
display = {
disable_document_color = false, -- keep vim.lsp.document_color active
},
},
})
要仅针对特定 LSP 服务器禁用:
require("colorizer").setup({
options = {
display = {
disable_document_color = { cssls = true, html = true },
},
},
})
或者改用内置功能,并禁用 colorizer 的 LSP 集成:
-- Let Neovim handle LSP colors, colorizer handles everything else
require("colorizer").setup({
options = {
parsers = {
tailwind = { enable = true, lsp = false },
},
},
})
内置的 vim.lsp.document_color.enable() 支持样式选项:'background'(默认)、'foreground'、'virtual' 或自定义字符串/函数。有关详细信息,请参阅 :help vim.lsp.document_color.enable()。
注意: 这仅适用于 Neovim 0.12+。Neovim 0.10 和 0.11 没有此功能,不受影响。
组合显示模式
display.mode 接受一个列表,以同时应用多种模式:
require("colorizer").setup({
options = {
display = {
mode = { "background", "virtualtext" }, -- colored background + color swatch
},
},
})
非虚拟文本模式(background、foreground、underline)会合并为单个扩展标记,因为它们的高亮属性不会重叠。virtualtext 始终使用自己的扩展标记。这四种模式的任意组合都是有效的。
注意:
background和foreground都会设置fg属性。当两者组合使用时,background优先级更高(为确保可读性,需要自动对比度文本)。如果希望同时实现两种效果,请使用background+underline。
高亮优先级
Colorizer 使用 display.priority 中的扩展标记优先级来控制当多个来源作用于同一范围时哪些高亮生效:
| 键 | 默认值 | 基于 | 用途 |
|---|---|---|---|
default |
150 | vim.hl.priorities.diagnostics |
基于解析器的常规高亮 |
lsp |
200 | vim.hl.priorities.user |
Tailwind LSP 高亮 |
这些默认值高于 treesitter(100)和语义标记(125),因此 colorizer 高亮始终优先于语法高亮。LSP 优先级高于默认值,因此在同一范围上,Tailwind LSP 结果优先于基于解析器的匹配。
Neovim 内置的 vim.lsp.document_color 未对其扩展标记设置显式优先级(实际优先级为 0),因此如果两者在同一缓冲区中激活,会出现重复高亮而非优先级冲突。这就是 disable_document_color 默认值为 true 的原因 — 它可以完全防止重复高亮。
自定义优先级的方法:
require("colorizer").setup({
options = {
display = {
priority = {
default = 50, -- lower than treesitter, color highlights lose
lsp = 300, -- higher than default user priority
},
},
},
})
自定义解析器
注册自定义解析器以高亮显示应用程序特定的颜色模式:
require("colorizer").setup({
options = {
parsers = {
custom = {
{
name = "android_color",
prefixes = { "Color." },
parse = function(ctx)
local m = ctx.line:match('^Color%.parseColor%("#(%x%x%x%x%x%x)"%)', ctx.col)
if m then
return #'Color.parseColor("#xxxxxx")', m:lower()
end
end,
},
},
},
},
})
每个自定义解析器支持:name、parse(ctx)、prefixes、prefix_bytes、setup(ctx)、teardown(ctx)、state_factory()。有关详细信息,请参阅完整文档。
提示: 自定义解析器可以声明
prefixes = { "=" }(或任何其他触发器),并在parse中验证其余部分。若要重用 xterm 256 色调色板而不复制它,可调用require("colorizer.parser.xterm").lookup_256(idx)或require("colorizer.parser.xterm").get_palette()。内置的ls_colors解析器已涵盖=38;5;NNN/=48;5;NNN和=38;2;R;G;B/=48;2;R;G;B代码段。
钩子
should_highlight_line 在解析每行之前调用。返回 true 以进行高亮,返回 false 以跳过:
require("colorizer").setup({
options = {
hooks = {
should_highlight_line = function(line, bufnr, line_num)
return string.sub(line, 1, 2) ~= "--"
end,
},
},
})
should_highlight_color 在颜色解析完成后调用。返回 false 可跳过该颜色:
hooks = {
should_highlight_color = function(rgb_hex, parser_name, ctx)
-- Skip black and white
return rgb_hex:lower() ~= "000000" and rgb_hex:lower() ~= "ffffff"
end,
}
transform_color 会在显示前重新映射颜色:
hooks = {
transform_color = function(rgb_hex, ctx)
-- Desaturate: convert everything to grayscale
local r = tonumber(rgb_hex:sub(1, 2), 16)
local g = tonumber(rgb_hex:sub(3, 4), 16)
local b = tonumber(rgb_hex:sub(5, 6), 16)
local gray = math.floor(0.299 * r + 0.587 * g + 0.114 * b)
return string.format("%02x%02x%02x", gray, gray, gray)
end,
}
on_attach 和 on_detach 会在 colorizer 附加到缓冲区或从缓冲区分离时被调用:
hooks = {
on_attach = function(bufnr, opts)
vim.notify("Colorizer attached to buffer " .. bufnr)
end,
on_detach = function(bufnr)
vim.notify("Colorizer detached from buffer " .. bufnr)
end,
}
CSS 自定义属性
css_var 解析器通过扫描缓冲区中 --name: <color> 定义来解析 var(--name) 引用。任何被已配置解析器识别的颜色格式(十六进制、rgb、hsl 等)都可用于定义中。
require("colorizer").setup({
options = {
parsers = {
css = true, -- also enables css_var via the css preset
},
},
})
或者在不使用完整 css 预设的情况下显式启用它:
require("colorizer").setup({
options = {
parsers = {
hex = { default = true },
css_var = { enable = true, parsers = { css = true } },
},
},
})
特性:
- 解析别名变量:追踪
--alias: var(--base)这类链式定义 - 支持
var(--name, fallback)语法(使用定义值进行高亮) - 追踪
@import声明以解析导入的 CSS 文件中的变量 - 每次文本变更时重新扫描定义
跨文件变量解析
css_var 会自动追踪 @import 声明,以解析其他文件中定义的变量。支持所有标准导入语法:
@import url("variables.css");
@import url('tokens.css');
@import "theme.css";
导入路径相对于当前文件进行解析。缓冲区本地定义始终优先于导入的定义。
Lua API
require("colorizer").attach_to_buffer(0, {
parsers = { css = true },
display = { mode = "foreground" },
})
require("colorizer").detach_from_buffer(0)
用户命令
| 命令 | 描述 |
|---|---|
| ColorizerAttachToBuffer | 附加到当前缓冲区 |
| ColorizerDetachFromBuffer | 停止高亮当前缓冲区 |
| ColorizerReloadAllBuffers | 重新加载所有已高亮的缓冲区 |
| ColorizerToggle | 切换当前缓冲区的高亮状态 |
旧版选项
平面的 user_default_options 格式完全受支持,并在内部自动转换为新的结构化格式。无需迁移。
重要提示:
- 旧版选项集已冻结——不会再向其中添加新选项。新功能(例如
hsluv、xcolor、css_var_rgb、css_var、debounce_ms、hex.hash_aarrggbb、hex.no_hash)仅可通过结构化的options格式使用。 - 如果同时提供了
options和user_default_options,则options优先。
require("colorizer").setup({
user_default_options = {
names = true,
RGB = true,
RRGGBB = true,
css = false,
mode = "background",
tailwind = false,
},
})
详见 :help colorizer.config 以及完整文档中的旧版至新版转换映射说明。
测试
make test
make test-file FILE=tests/test_config.lua
文档
:help colorizer- 完整 API 文档