vim - What's the best way to set statusline color to change, based on mode

3

1

I've tried to improve my vim experience trying to have the vim statusline color to change everytime the mode changes.

I've triend this: (found here)

    "Automatically change the statusline color depending on mode
function! ChangeStatuslineColor()
  if (mode() =~# '\v(n|no)')
    exe 'hi! StatusLine ctermfg=008'
  elseif (mode() =~# '\v(v|V)' || g:currentmode[mode()] ==# 'V·Block' || get(g:currentmode, mode(), '') ==# 't')
    exe 'hi! StatusLine ctermfg=005'
  elseif (mode() ==# 'i')
    exe 'hi! StatusLine ctermfg=004'
  else
    exe 'hi! StatusLine ctermfg=006'
  endif

  return ''
endfunction

...and include:

set statusline+=%{ChangeStatuslineColor()} 

But there's an issue, if you switch to insert mode and then press Esc to come back to normal mode, it doesn't change back the color. It'll change back the color only when you manually enter a different mode.

rossijonas

Posted 2018-01-14T13:32:18.023

Reputation: 33

Answers

0

Dynamically modifying the StatusLine highlight group is the wrong approach. This overrides any presets of your colorscheme, and it doesn't seem to work (maybe the addition of a :redraw[status] would help, but that would make it an even uglier implementation).

Vim allows to specify a custom highlight group in the statusline (even multiple), with the %#HLname# symbol. See :help 'statusline' for details. You can either dynamically change the 'statusline' value (this also allows different colors for different statuslines), or use the %!MyStatusLine() approach that re-evaluates the value itself.

Ingo Karkat

Posted 2018-01-14T13:32:18.023

Reputation: 19 513

Thank you for this answer! Seems to be a better solution indeed. I'll get my head on it, thanks – rossijonas – 2018-01-25T15:36:10.643