Go to the Current Colorscheme section
to see what each color stands for.
(Unfortunately placing it here breaks syntax
highlighting for all multi-line code blocks below it)
With lazy.nvim
{
"lukoshkin/highlight-whitespace",
config=true,
}
With packer.nvim
use "lukoshkin/highlight-whitespace"
With vim-plug
Plug 'lukoshkin/highlight-whitespace', { 'branch': 'vimscript' }
One can adapt the installation code for other plugin managers!
Two ways to configure depending on the selected branch
master (Lua)
For the Lua implementation, the functionality is much wider.One can specify a color for each pattern and per filetype. It can be a regular color name or hex code.
lazy.nvim
{ "lukoshkin/highlight-whitespace", opts = { tws = "\\s\\+$", clear_on_winleave = false, palette = { markdown = { tws = 'RosyBrown', ['\\S\\@<=\\s\\(\\.\\|,\\)\\@='] = 'CadetBlue3', ['\\S\\@<= \\{2,\\}\\S\\@='] = 'SkyBlue1', ['\\t\\+'] = 'plum4', }, other = { tws = 'PaleVioletRed', ['\\S\\@<=\\s,\\@='] = 'coral1', ['\\S\\@<=\\(#\\|--\\)\\@<! \\{2,3\\}\\S\\@=\\(#\\|--\\)\\@!'] = 'LightGoldenrod3', ['\\(#\\|--\\)\\@<= \\{2,\\}\\S\\@='] = '#3B3B3B', ['\\S\\@<= \\{3,\\}\\(#\\|--\\)\\@='] = '#3B3B3B', ['\\t\\+'] = 'plum4', } } } }
packer.nvim
use { 'lukoshkin/highlight-whitespace', config = function () require'highlight-whitespace'.setup { tws = '\\s\\+$', clear_on_winleave = false, palette = { markdown = { tws = 'RosyBrown', ['\\S\\@<=\\s\\(\\.\\|,\\)\\@='] = 'CadetBlue3', ['\\S\\@<= \\{2,\\}\\S\\@='] = 'SkyBlue1', ['\\t\\+'] = 'plum4', }, other = { tws = 'PaleVioletRed', ['\\S\\@<=\\s,\\@='] = 'coral1', ['\\S\\@<=\\(#\\|--\\)\\@<! \\{2,3\\}\\S\\@=\\(#\\|--\\)\\@!'] = 'LightGoldenrod3', ['\\(#\\|--\\)\\@<= \\{2,\\}\\S\\@='] = '#3B3B3B', ['\\S\\@<= \\{3,\\}\\(#\\|--\\)\\@='] = '#3B3B3B', ['\\t\\+'] = 'plum4', } } } end }
tws
- main pattern for trailing whitespace
clear_on_winleave
- clear highlighting when switching to another window
vimscript
Note you must specify bothctermbg
and guibg
values,
even if you don't care about one of them. Specifying other than
bg
keys has no effect.
let g:tws_pattern = '\s\+$'
let g:tws_color_md = { 'ctermbg': 138, 'guibg': 'RosyBrown' }
let g:tws_color_any = { 'ctermbg': 211, 'guibg': 'PaleVioletRed' }
-
✔️
It is possible to highlight tabs by specifyingpatterns = { '\\s\\+$', '\\t\\+' }
.
In future patches, the customization will also allow setting a color for each pattern, e.g., in the palette table:palette = { python = {['\\s\\+$'] = 'PaleVioletRed', ['\\t\\+'] = 'plum4'} }
-
I have a function for trimming trailing whitespace in my "vimrc" configuration. Adding a similar one for trimming any unwanted whitespace (including tabs and etc.) to the plugin is under the question.
-
The approach with
matchadd
+matchdelete
+nvim_set_hl
might be not the most optimal. One of the other options to test will be looping over each line of a document looking for a pattern (lua'sstring.find
or vim'smatchstrpos
) and then dynamically adding highlighting withnvim_buf_add_highlight
.
Why to create another HWS plugin?
(I refer to my plugin as HighlightWS
, and similar others ─ as HWS.)
HighlightWS
leveragesmatchadd
instead ofmatch
.- Trailing whitespace in markdown files is highlighted with a different color.
- Written in Lua. Vimscript version is also available.
- I was configuring my vimrc, I didn't know about existing plugins.
- It is highly customizable!
Let's talk about each point separately.
-
match
function provides for creating just three matches (i.e., usingmatch
,2match
,3match
). Better not to occupy them, if one can achieve the same goals withmatchadd
. -
Trailing whitespace in markdown files is not a bad thing and sometimes is even a necessity. Thus, more mild colors than red should be used.
-
Though the topic of Lua in Neovim is trending and may seem overhyped in some aspects, the fact that it is a Lua plugin is convenient for those who are not familiar with VimL and definitely handy for Lua programmers. Note there is also a Vimscript version on a separate branch for plugin managers that do not treat Lua code (it is a bit outdated, though).
-
Don't do that. I was not going to create a new plugin, though I ended up writing it. Googling trailing whitespace vim terminated on the first link . By the way, the mentioned there tips are just 'tips', as written.
-
Actually, it is much wider than just highlighting whitespace. You can come up with your own pattern and color specifically for each filetype.
This section refers to the GIF at the README beginning