Create an autocommand to change the background of inactive windows

I already have the initial idea

vim.api.nvim_create_autocmd('WinEnter', {
  pattern = '*',
  callback = function() 
       "normal color for background"
  end,
})
vim.api.nvim_create_autocmd('WinLeave', {
  pattern = '*',
  callback = function(args) 
       "please dim the background for inactive window"
  end,
})

And I also have this idea using vimscript

" source: https://www.reddit.com/r/vim/comments/d46ger/comment/f084pjk/
hi ActiveWindow guibg=#21242b
hi InactiveWindow guibg=#282c34

" Call method on window enter
augroup WindowManagement
  autocmd!
  autocmd WinEnter * call Handle_Win_Enter()
augroup END

" Change highlight group of active/inactive windows
function! Handle_Win_Enter()
  setlocal winhighlight=Normal:ActiveWindow,NormalNC:InactiveWindow
 endfunction

From the same reddit page we have:

That can be shortened. Because NormalNC automatically applies to the “Non-Current” window, you don’t need a WinEnter autocmd. Just use :set instead of :setlocal.

hi ActiveWindow ctermbg=None ctermfg=None guibg=#21242b
hi InactiveWindow ctermbg=darkgray ctermfg=gray guibg=#282c34
set winhighlight=Normal:ActiveWindow,NormalNC:InactiveWindow
1 Like

Here is how to add the condition when focusing between vim and tmux in vimscript.

hi ActiveWindow guibg=#21242b
hi InactiveWindow guibg=#282c34

augroup WindowManagement
  autocmd!
  autocmd WinEnter * call Handle_Win_Enter()
  autocmd FocusLost * call Handle_Focus_Lost()
  autocmd FocusGained * call Handle_Focus_Gained()
augroup END

function! Handle_Win_Enter()
  setlocal winhighlight=Normal:ActiveWindow,NormalNC:InactiveWindow
endfunction

function! Handle_Focus_Lost()
  setlocal winhighlight=Normal:InactiveWindow,NormalNC:InactiveWindow
endfunction

function! Handle_Focus_Gained()
  setlocal winhighlight=Normal:ActiveWindow,NormalNC:InactiveWindow
endfunction