2

I use several splits (windows). To quickly identify the splits that has the focus I would like Vim to auromatically adapt the background color of the active split.

Is there a way to do so?

Is it possible to have a different definition of the highlighting group Normal depending if the split (window) is active or not?

enter image description here

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37

3 Answers3

3

You can use :h wincolor for this: https://asciinema.org/a/od7Eh6sVZdw9r65vUrtctZ0PC

Change non-active windows background:

hi DimNormal guibg=#203040

augroup ActiveWin | au! au WinEnter * setl wincolor= au WinLeave * setl wincolor=DimNormal augroup END

enter image description here

Of course, you can do the opposite -- change active window background:

hi NormalActive guibg=#203040

augroup ActiveWin | au! au WinEnter * setl wincolor=NormalActive au WinLeave * setl wincolor= augroup END

Maxim Kim
  • 13,376
  • 2
  • 18
  • 46
  • 1
    Thanks Maxim! It looks like a charm :-) – Vivian De Smedt Mar 17 '23 at 11:34
  • your solution work quite well but I have a problem with some windows where the WinEnter event seems not to be triggered (at least the color remain black). If you open the quickfix window (:copen) it works well, if you close it (:cclose) it works too but if you reopen it (:copen) it remains dimmed (I have the same problem with the NERDTree drawer. Is there another event to solve the problem (I tried WinNew but without success)? Is there something else todo? – Vivian De Smedt Mar 24 '23 at 07:49
1

Another useful mapping (which I have on <leader><leader><leader>, or 3 spaces for me) is to "blink" the current cursor location in an obvious way:

" ~/.vim/autoload/bk.vim
function bk#cursor#blink(time) abort
  call bk#cursor#hl_on()
  let time_in_ms = float2nr(a:time * 1000)
  if has('timers')
    let _ = timer_start(time_in_ms, {_ -> bk#cursor#hl_off()})
  else
    exec 'sleep'  time_in_ms  'm'
    call bk#cursor#hl_off()
  endif
endfunction

function bk#cursor#hl_on() abort set cursorline cursorcolumn redraw endfunction

function bk#cursor#hl_off() abort set nocursorline nocursorcolumn redraw endfunction

Along with

nnoremap <leader><leader><leader> :call bk#cursor#blink(0.2)<enter>

(Adjust timing as needed.)

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65
1

Here is a small variation from the solution of @Maxim that seems more robust:

hi DimNormal guibg=#203040

augroup ActiveWin | au! au WinEnter,BufEnter * setl wincolor= au WinLeave,BufLeave * setl wincolor=DimNormal augroup END

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37