13

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()

Highlight trailing whitespaces

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:◦

enter image description here enter image description here

Pros:

  • Easy to use
  • Fast

Cons:

  • Issue if :set cursorline enabled
  • Dedicated color for list/trail is 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=red

enter image description here enter image description here

Pros:

  • 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

Which 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
  • If you don't like red, then choose a different color. If you want the color to be compatible with different color schemes, then link to a color group name instead of using a particular color, e.g., :hi link ExtraWhitespace CursorLine. See :help hi-link.
    – garyjohn
    May 31, 2015 at 17:41

1 Answer 1

4

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
  • So, from your answer I understand that there is so solution that allow using a custom Unicode char to mark extra spaces. This question is indeed more tricky that I initially thought.
    – nowox
    May 31, 2015 at 19:12
  • Right, custom chars only work with 'listchars'. To me, the highlighting is more than enough (especially with aggressive colors). May 31, 2015 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .