I would like to use two different backgrounds colors bg1 and bg2 with:

  1. bg1 for all text on a line (possibly nothing if line is empty) up to the end-of-line,
  2. bg2 for the empty area after the end-of-line,
  3. while keeping hlsearch working,
  4. and syntax highlighting working.

For example, if displaying <empty_area> as _'s starting with # on a 16-character-wide window, with "text" as the search pattern and a syntax recongizing if, it would look like:

    if "text"#______


  • anything but the trailing _'s with a different background (here black foreground)...
  • ...than the trailing _'s (here gray foreground),
  • except for "text" which should be highlighted as the Search group (here green foreground),
  • and if which has the syntax highlighting of the chosen language (here blue foreground).

I have tried this in my vimrc:

hi Normal     guibg=Gray75
hi Contents   guibg=LightGray
match Contents /.\+.\@!/.

but then I lose the pattern match highlighting for hlsearch, which is I guess superseded by the Contents match.

Is there a proper way to achieve what I want: different backgrounds for text and empty area after EOL, with hlsearch and syntax highlighting still working ?

PS: The NonText group is not relevant, since it links to the empty area after the buffer, and not after each line.

  • The usual (and UI-agnostic) way to visualize whitespace would be :set list. While it may not look quite as good, it was built exactly for this use case.
    – Friedrich
    Commented Jan 29 at 13:49
  • Indeed, the list option can show an EOL marker that might suffice. Would that work gilgron?
    – D. Ben Knoble
    Commented Jan 29 at 22:25
  • Adding a character at EOL does not fit to my eye, but allowed me to find my solution. Thanks @d-ben-noble and @friedrich.
    – gilgron31
    Commented Jan 30 at 9:52
  • I guess the lcs (listchars) variable was not judiciously set on the file I tried :set list with. In fact, with set lcs=tab:> ,trail:-,extends:>,precedes:<,nbsp:+, I get a nice complement to the 2match solution... Thanks again.
    – gilgron31
    Commented Jan 30 at 13:51

2 Answers 2


I tried adding those lines to my .gvimrc file:

    hi Normal     guibg=Gray75
    hi Contents   guibg=LightGray
    match Contents /.\+.\@!/.
    noremap * *N:2mat Search /<C-R>//<C-M>n

which solves the hlsearchissue. But the highlighting involving background, like with diffvim, gets broken. So I fell back on this lighter solution:

    hi Normal       guibg=Snow2 guifg=NONE
    hi Contents     guibg=Snow1 guifg=NONE
    2match Contents /$/

which highlights the first non-character at eols and is finally noticeable enough.


I would do:

highlight SpecialKey guibg=LightGray

highlight Conditional guibg=LightGray
highlight Content guibg=LightGray
highlight Exception guibg=LightGray
highlight Function guibg=LightGray
highlight Include guibg=LightGray
highlight Operator guibg=LightGray
highlight Repeat guibg=LightGray
highlight Statement guibg=LightGray
highlight Structure guibg=LightGray
highlight Type guibg=LightGray

highlight Content guibg=LightGray
autocmd FileType * syntax match Content /\S\+/
  • 1
    This prevents any syntax highlighting, which is an unwanted effect. I have modified the question to include this requirement.
    – gilgron31
    Commented Jan 29 at 17:35
  • I have proposed another solution that is working fine at least with Python syntax at my end. Commented Jan 30 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.