I would like to have a nice and sweet display of trailing white-space in my terminal vim. This would allow me to remove them before closing the file.
I am aware that this question is quite popular and I already found dozens of answers on SO. Unfortunately I did not found a satisfying one yet...
So I will try to summarize the gathered information and the solutions I found.
Solution 1: Manually highlight trailing spaces
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()Pros:
- Easy to use
- Fast
- Compatible with
cursorline
Cons:
- Chosen color not compatible with other color schemes because color is manually set to
red - Big red background blocks are not very esthetic
Solution 2: Using list feature
set list
set listchars+=trail:◦Pros:
- Easy to use
- Fast
Cons:
- Issue if
:set cursorlineenabled - Dedicated color for
list/trailis not configurable. Has to be the same as the other listchars
Solution 3: Using conceal feature
syn match WhiteSpace "\s\+$" containedin=ALL conceal cchar=◦
set conceallevel=2
set concealcursor=nv
highlight Conceal ctermfg=redPros:
- Prettier than the first solution
Cons:
- Background is not the same as the cursorline background
- May interfere with other conceal rules
- Chosen color not always compatible with the color scheme
- Not compatible with older version of Vim
My question
1Which one of the enumerated solutions is the most suitable in terms of performance and compatibility and how can I get all the pros and none of the cons?
In other terms, I would like to find a solution where:
- The highlight trailing color agree with the chosen colorscheme
- This color is clearly visible but not too aggressive to the eyes
- Compatible with
cursorline(background and foreground)`- Compatible with other plugins that may use conceal
- The trailing char can be set to any Unicode char
1 Answer
In terms of performance, none of these should make a difference. Vim mostly gets bogged down by complex syntax patterns; the pattern here is quite simple.
To make the highlight color agree with the chosen colorscheme, use :hi link to an existing group; usually, that would be Error. Of course, any of this only matters if you switch (quite different) colorschemes often; else, using suitable colors directly (as in your solution 1) would be fine, too.
Solution 1: Manually highlight trailing spaces
I'd go for this, but using matchadd() instead of :match; this leaves the latter for custom highlighting. Also, clearmatches() is too coarse; it deletes all matches, and therefore may affect plugins. You can use / have a look at my ShowTrailingWhitespace plugin for a clean and powerful implementation (that also includes exceptions for certain filetypes). Yes, it's a much bigger solution, but I haven't notice any impact yet.
Solution 2: Using list feature
This is a useful feature in itself, and keeping it on permanently has effects on cursor positioning on Tab characters, so I wouldn't recommend that.
Solution 3: Using conceal feature
That will surely interfere with some syntaxes and plugins using the feature. Bad idea.
2