Friday, 1 March 2019

Highlight active window in tmux and neovim


With my recent upgrade to [[Vim Neovom|Neovim]]. I was able to implement a really nice features that integrate with tmux. I am now able to seamlessly highlight the active split with a lighter background color.

The first thing you need to do is set up your tmux.conf. With [[tmux]] I set the window style darker and then lighten the active window. We will also need to turn on focus-events for later to let vim know when you have moved from a tmux pain into a vim buffer.

set -g window-style 'bg=colour235'
set -g window-active-style 'bg=colour0'

set -g focus-events on

For the .vimrc or vim.init I have set the ActiveWindow and InactiveWindow this dose most of the work by doing exactly what we want. However, when moving from vim into tmux it doesn't deactivate the window. So we can use the focus events to set the background of the window on FocusLost and then reset it again on FocusGained. I have wrapped all of this in a has('nvim') so I can keep the vim config working.

"
" Highlight the active window even play nice with tmux splits
"
if has('nvim')
    hi ActiveWindow ctermbg=00 | hi InactiveWindow ctermbg=235
    set winhighlight=Normal:ActiveWindow,NormalNC:InactiveWindow

    au VimEnter,WinEnter,BufEnter,BufWinEnter,FocusGained * hi ActiveWindow ctermbg=00 | hi InactiveWindow ctermbg=235
    au VimLeave,WinLeave,BufLeave,BufWinLeave,FocusLost * hi ActiveWindow ctermbg=235 | hi InactiveWindow ctermbg=235
else
    hi Normal ctermbg=None
endif