3

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

1
  • check :h wincolor
    – Maxim Kim
    Commented Mar 17, 2023 at 11:15

3 Answers 3

4

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
2
  • 1
    Thanks Maxim! It looks like a charm :-) Commented Mar 17, 2023 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? Commented Mar 24, 2023 at 7: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.)

1
  • @Ben, thanks for the tip :-) Commented Mar 17, 2023 at 14:16
1

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

hi DimNormal guibg=#1b212c
hi DimConsole guifg=#d8dee9 guibg=#1b212c

function! DimWindow()
  if getwinvar(winnr(), '&diff')==1
    return
  endif
  if getwininfo(win_getid())[0].terminal==1
    setlocal wincolor=DimConsole
  else
    setlocal wincolor=DimNormal
  endif
endfunction

augroup ActiveWin | au!
  au WinEnter,BufEnter,BufWinEnter * setlocal wincolor=
  au WinLeave,BufLeave * call DimWindow()
augroup END

I have adapted to:

  • Avoid to have the behavior when you diff two buffers
  • Terminal windows that seems to behave slightly differently (at least on Windows)

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.