vimstatusline

Why statuslines missing color after change colorscheme?


My vimrc is posted at the bottom.
I have not used any plugins related to statusline.
This file can work normally.
But when I change colorscheme by commandline, like ":colorscheme evening"
The statusline missing color.
Resource vimrc can fix bug, like ":source $HOME_vimrc".
Same bug happened in windows10 and Debian Testing.

Is it a bug?

colorscheme desert
"""""""""""""""""
set laststatus=2
""""""""""""""""""
function! InsertStatuslineColor(mode)
if a:mode == 'i'
  hi statusline guibg=red
elseif a:mode == 'r'
  hi statusline guibg=blue
else
  hi statusline guibg=black
endif
endfunction
au InsertEnter * call InsertStatuslineColor(v:insertmode)
au InsertLeave * hi statusline guibg=orange guifg=white
hi statusline guibg=gray guifg=black
""""""""""""""""""
set statusline=
set statusline +=%4*%n%m%r%h%w%*
set statusline +=%F
set statusline +=%=%1*[%2*%{&ff}:%{&fenc!=''?&fenc:&enc}%1*]
set statusline +=[%2*%Y%1*]
set statusline +=[%2*%03v:%03l%1*/%3*%L(%p%%)%1*]
hi User1 guifg=gray
hi User2 guifg=green
hi User3 guifg=white
hi User4 guifg=red
hi User5 guifg=#a0ee40

Solution

  • Not a bug but a feature. Any Vim "colorscheme" is simply a VimScript file which in 99,99% cases starts with the command

    hi clear
    

    And as you may guess, hi clear cancels the effect of all previous :hi commands, including yours :hi UserN ...

    So you should either

    1. Trap colorscheme event to redefine your UserN every time the colorscheme has been changed;

    2. Or use only standard color groups (%#name#) which, hopefully, are defined in every colorscheme out there. See :h highlight-groups for the list.

    Of course, in the latter case your status line will look a bit differently under different colorschemes.