1

My vimrc sets the buffer number in the statusline with a different color background based on if it's readonly or modified.

I'd also like to set a different color if the buffer is not in the active window (useful when in split window mode).

I am using &mod and &readonly to detect for modified or read-only. How can I check for whether the buffer is in the active window? (i.e. maybe change it to gray if the window is inactive).

Here's the code I'm using for read-only and modified:

function! StatusLineHeader()
    return '  '.bufnr('%').' '
endfunction

set statusline=
set statusline+=%#UWhiteOnBlue#%{&mod?'':&readonly?'':StatusLineHeader()} "default header
set statusline+=%#UWhiteOnOrange#%{&mod?'':&readonly?StatusLineHeader():''} "readonly header
set statusline+=%#UWhiteOnGreen#%{&mod?StatusLineHeader():''} "modified header
tomocafe
  • 1,425
  • 4
  • 20
  • 35

1 Answers1

-3

First of all, generally it's a bad idea to use too many colors in the status line. You'd really better drop to predefined StatusLine / StatusLineNC / %* which are set up automatically, i.e.

set statusline+=%*%{StatusLineHeader()} "default header

But if you really want it, you need :h g:actual_curwin. So it becomes:

set statusline+=%#StatusLine#%{g:actual_curwin==win_getid()?StatusLineHeader():''}
set statusline+=%#StatusLineNC#%{g:actual_curwin!=win_getid()?StatusLineHeader():''}

Note: g:actual_curwin was added in Vim 8.1.1372

Matt
  • 13,674
  • 1
  • 18
  • 27