How to set background color for Vim's active window only?

Solution 1:

Heptite, thank you for the idea.

I just replace WinEnter and WinLeave autocmd's to add / remove line numbers in front of each line:

augroup BgHighlight
    autocmd!
    autocmd WinEnter * set number
    autocmd WinLeave * set nonumber
augroup END

Now is more stylish to track which window is active.

Solution 2:

The highlight groups that control the default background color are "Normal" and "NonText". Unfortunately these are "global" highlights and cannot be "localized" on a per-window basis.

However, here is a truly ugly kludgy example:

augroup BgHighlight
    autocmd!
    highlight ActiveWindow guibg=lightblue
    autocmd WinEnter * call matchadd('ActiveWindow', '.*', 10, 1682)
    autocmd WinLeave * call matchdelete(1682)
augroup END

doautocmd BgHighlight WinEnter -

Since this is just an example, you'll have to modify this to suit your individual needs, such as adding a ctermbg=... highlight along with the guibg=...

The drawback is that this will only highlight the background of existing text in the files, not the whole line/window. (Like I said, ugly and kludgy.)