whitespace.nvim - highlight whitespace on demand
require("whitespace").setup()
this uses default settings, which is equivalent to:
require("whitespace").setup({
excluded_filetypes = {
},
excluded_buftypes = {
},
definition = {
{
id = "trail",
pattern = [[\s\+$]],
pattern_insert = [[\s\+\%#\@<!$]],
default_display = true,
},
},
})
vim.api.nvim_set_hl(0, "trail", {ctermbg = 1, bg = "#ff0000"})
this highlights trailing whitespaces, but it won't highlight them if the cursor is placed at the end in insert mode
\s\+$
and \s\+\%#\@<!$
are both vim patterns
require("whitespace").setup({
excluded_filetypes = {
"c",
"lua",
},
excluded_buftypes = {
".+",
},
})
this highlights the whitespace, but only when:
- the
filetype
is neitherc
norlua
- the
buftype
does not match lua pattern.+
, which means thebuftype
must be an empty string
require("whitespace").setup()
local toggle = function()
vim.w.trail = not vim.w.trail
require("whitespace").match_sync()
end
vim.keymap.set({"n", "x", "i"}, "<f11>", toggle)
this defines a function named toggle
, which toggles the display of trailing whitespaces
require("whitespace").setup({
excluded_filetypes = {
},
excluded_buftypes = {
".+",
},
definition = {
{
id = "trail",
pattern = [[\s\+$]],
pattern_insert = [[\s\+\%#\@<!$]],
default_display = true,
},
{
id = "midmultispacetab",
pattern = [[\S\zs\s\{2,}\ze\S]],
default_display = true,
},
{
id = "space",
pattern = [[ ]],
default_display = false,
},
{
id = "tab",
pattern = [[\t]],
default_display = false,
},
},
})
vim.api.nvim_set_hl(0, "trail", {bg = "#0000ff"})
vim.api.nvim_set_hl(0, "midmultispacetab", {bg = "#ff0000"})
vim.api.nvim_set_hl(0, "space", {bg = "#0000ff"})
vim.api.nvim_set_hl(0, "tab", {bg = "#ffff00"})
local toggle = function()
vim.w.space = not vim.w.space
vim.w.tab = not vim.w.tab
require("whitespace").match_sync()
end
vim.keymap.set({"n", "x", "i"}, "<f11>", toggle)
you might be able to guess what this setup does
if you are using lazy.nvim
:
{
"aidancz/whitespace.nvim",
lazy = false,
config = function()
require("whitespace").setup()
end,
}
you may use this sample file to test:
http://xahlee.info/emacs/emacs/emacs_init_whitespace_mode.html
above is beginning of file repeated newline
beginning single space
ending single space
beginning repeated spaces
beginning repeated spaces
ending repeated spaces
ending repeated spaces
beginning single tab
ending single tab
beginning repeated tabs
ending repeated tabs
beginning space tab
ending space tab
beginning space space tab
ending space space tab
beginning tab space
ending tab space
beginning tab space space
ending tab space space
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
this is a long line. what's “long” is determined by fill-column, not by window width.
long long long long long long long long long long
LongLineNoWhiteSpacettttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
this is a long line with beginning and ending repeated spaces long long long long
following is end of file repeated newline