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

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

A small but visually pleasing feature, and slightly adding to usability, I'd like to have Vim use different background color for the active window.

Here's a sketch of what I am after:

ここに画像の説明を入力してください

Normally that vim would have all the background in black, but if only the active window had its own color to highlight user's attention. Highlighting just the statusbar only isn't enough!

答え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.

答え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.)

答え3

I was looking for the same, and discovered (through this issue https://github.com/vim/vim/issues/2686) that this is now supported through the wincolor (https://vimhelp.org/options.txt.html#%27wincolor%27) option, which can be set locally (setl) so i added this in my vimrc.

" different color depending on focus
hi NormalNC ctermbg=234
augroup winbg
    autocmd!
    au WinEnter * setl wincolor=
    au WinLeave * setl wincolor=NormalNC
augroup END

which gives me this result (yeah I'm not looking for a very big difference, but trust me it changes everything).

ここに画像の説明を入力してください

答え4

Adding on the the solution provided by @Tshirtman, I just modified that customization to this and it seemed to look very nice while switching between split panes (just sharing the same):

    hi NormalNC ctermbg=236 ctermfg=102
    augroup winbg
        autocmd!
        au WinEnter * setl wincolor= syn=on
        au WinLeave * setl wincolor=NormalNC syn=off
    augroup END

関連情報