Why does Vim only activate the highlight configuration after I source the .vimrc manually

vimvimrc

I was cleaning up my vimrc today, splitting it into separate files to make it easier to maintain. On completion of these changes, all my configuration, mappings and plugins appear to be working, except for highlighting trailing whitespace.

I have the following lines in .vim/rc/appearance.vim:

highlight RedundantSpaces term=standout ctermbg=red guibg=red
match RedundantSpaces /\s\+$\| \+\ze\t/ "\ze sets end of match so only spaces highlighted

As I understand it, the first line creates a highlight group 'RedundantSpaces', and instructs vim to make the background of any text matching the highlight group red, when on a colour terminal. The second line defines the pattern used to identify text matching the highlight group.

When I open a file in vim and start typing spaces on a blank line, they are not highlighted in red. If I run :source ~/.vimrc, the highlighting appears.

If I remove the lines sourcing all my individual rc/*.vim files from .vimrc and put the above two RedundantSpaces lines in .vimrc directly, vim behaves as expected, and trailing whitespace is highlighted.

However, if I leave in the lines sourcing rc/*.vim, and then have the RedundantSpaces lines at the end of my .vimrc (i.e. those commands are the last to be run when vim is invoked), the highlighting still does not work, and I need to source ~/.vimrc.

It seems clear that something in my rc/*.vim files is interacting poorly with the highlighting configuration, however I'm at a loss to explain what that is. Nothing in any of those files should be interacting with the highlight configuration.

Can anyone suggest what might be going wrong here?

Best Answer

The issue is caused by the sensible plugin:line 93. :help t_Co

" Allow color schemes to do bright colors without forcing bold.
if &t_Co == 8 && $TERM !~# '^linux'
  set t_Co=16  " << --- Causes hickup
endif

If you run vim in verbose log mode (vim -V15load_log.vim) – and search for t_Co= and RedundantSpaces you'll see that sensible is actually parsed after your appearance.vim file.

Quick fix would be to either comment out that section of the code and add it to your .vimrc, but as you link to git repositories that is perhaps not what you want.

A different approach would be to move it out of the bundledirectory and add it as a separate source line in your vimrc, before your glob loop.

I.e.:

  source ~/.vim/hacks/vim-sensible/plugin/sensible.vim

  for f in split(glob ...

Another way would be to load custom highlight on BufLoad.

Etc. …

Related Question