14

I would like to highlight (part of) my statusline with %1*, for example:

set statusline=%1*%f%0*
highlight User1 ctermbg=0 ctermfg=10 cterm=bold

However, this always shows up as "empty" (the default terminal colours):

enter image description here

If I type :highlight User1, it shows User1 xxx cleared, and if I type :highlight User1 ctermbg=0 ctermfg=10 cterm=bold in the command window, it does seem to work.

How can I put this in my .vimrc file?

My full vimrc file I used for testing:

set nocompatible
set background=light
colorscheme default
set laststatus=2
set statusline=%1*%f%0*

highlight User1 ctermbg=0 ctermfg=10 cterm=bold
muru
  • 24,838
  • 8
  • 82
  • 143
Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271

1 Answers1

17

The problem is that many (all?) colorschemes will clear all highlights with highlight clear before setting their own colours. For example from /usr/share/vim/vim74/colors/peachpuff.vim:

" First remove all existing highlighting.
set background=light
hi clear
if exists("syntax_on")
  syntax reset
endif

let colors_name = "peachpuff"

hi Normal guibg=PeachPuff guifg=Black

hi SpecialKey term=bold ctermfg=4 guifg=Blue
" ... Many more highlights follow ...

This also clears the User1..9 groups (which doesn't make a lot of sense, IMHO).

The solution is to put custom User1..9 highlights in the ColorScheme autocmd. From :help ColorScheme:

After loading a color scheme. :colorscheme The pattern is matched against the colorscheme name. <afile> can be used for the name of the actual file where this option was set, and <amatch> for the new colorscheme name.

So instead of using a "bare" highlight command, use:

autocmd ColorScheme *
        \ highlight User1 ctermbg=0 ctermfg=10 cterm=bold |
        \ highlight User2 ctermbg=0 ctermfg=9 cterm=bold

If you frequently switch colorschemes, you can even use:

autocmd ColorScheme colorscheme_one_name  highlight User1 ctermbg=0 ctermfg=10 cterm=bold
autocmd ColorScheme another_scheme        highlight User1 ctermbg=0 ctermfg=11 cterm=bold

to get colours matched to specific colorschemes.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271