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

16

5

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:

enter image description here

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!

nperson325681

Posted 2010-10-08T09:01:28.280

Reputation: 1 401

Answers

8

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.

t0d0r

Posted 2010-10-08T09:01:28.280

Reputation: 131

This is cool. Too bad I don't fancy line numbers much so it's too unaesthetic. – nperson325681 – 2011-02-03T09:00:07.887

3

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

Heptite

Posted 2010-10-08T09:01:28.280

Reputation: 16 267

Nice thinking, but the pattern .* only matches the text of a file; nothing from the rest of the background. In addition, I only got it working in terminal with ctermbg property. Weird... – nperson325681 – 2010-10-09T08:01:19.040

Sorry, I should've been more clear in my answer. I have edited it. – Heptite – 2010-10-09T22:10:07.200