Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reloading vimrc causes different syntax highlighting

Tags:

vim

When I open up a file in macvim it is like this http://imgur.com/a/3cLqB#0. I have set ,V to :source ~/.vimrc<CR>.

After I have this file open, I press ,V, and the syntax highlighting changes to this http://imgur.com/a/3cLqB#1. The difference is that (,),;,, become from blue, white, and ->,.,? become from blue, darker blue. Why does that happen? This is my vimrc file https://gist.github.com/pvinis/4979592

--
Update: I found out that Valloric/vim-operator-highlight is the plugin that changes the colors. so the first picture is the correct picture. I also found out, that as soon as i do :syntax on, the colors reset. Is there a way to check if syntax is already on?

like image 939
pvinis Avatar asked Feb 18 '13 18:02

pvinis


3 Answers

Problem:

When reloading .vimrc, some highlight groups are messed up. It depends on what plugins you have and what colorschemes you are using. I've noticed that some highlight links were broken, and some highlight groups were cleared.

Affected highlight groups

In my particular setup I've noticed broken hi links or cleared groups on:

  • SignColumn
  • GitGutter (which uses SignColumn)
  • powerline-status (which uses status line)

Notice the affected areas after reloading:
(reloading means saving the modified file. Using :wa in this example) Reloading mess!

Solution

Unfortunately the listed answers or any combination of options that I've tried does not preserve or reinstate the hi groups after reloading. Running manually colorscheme <your-coloscheme> after reloading fixes everything, but when doing it with Vimscript it does not.
Hopefully someone will share a proper solution to this annoying small issue.

Ugly hack

Reloading vimrc:

On any change to my vim config files, I execute reload.vim:
.vimrc:

" .....
augroup reload_vimrc " {
    autocmd!
    autocmd BufWritePost ~/.vim/*.vim,~/.vim/vimrc source ~/.vim/reload.vim
augroup END " }

reload.vim: restores broken links and cleared groups

What we have to do is to reinstate the hi groups after sourcing vimrc.
To find the correct values for an affected area, e.g. for SignColumn, type:
:hi SignColumn, BEFORE any reloading has occurred.

The outcome is (where xxx is a preview): hl SignColumn

You have to do this for every affected hi.
In the following snippet I initially fix SignColumn to match my solarized colorscheme.
Then I fix some GitGutter color issues:
e.g., GitGutterAdd is linked to GitGutterAddDefault which is preserved, but from GitGutterAddDefault to DiffAdd is broken, so I reinstall that one. And so on so forth.

reload.vim:

source ~/.vim/vimrc

hi SignColumn ctermfg=12 ctermbg=0 guifg=Cyan guibg=Grey

" GitGutterAdd -> GitGutterAddDefault (preserved)
hi link GitGutterAddDefault DiffAdd

" GitGutterChange -> GitGutterChangeDefault (preserved)
hi GitGutterChangeDefault ctermfg=3 ctermbg=0 guifg=#bbbb00

" GitGutterDelete -> GitGutterDeleteDefault (preserved)
hi GitGutterDeleteDefault ctermfg=1 ctermbg=0 guifg=#ff2222

" GitGutterChangeDelete -> GitGutterChangeDefault (preserved)
" (which we already fixed above)

" Powerline highlight groups
" (see this attached Gist for solution)

Everything works as it should: Fix hi groups and links

Fixing powerline-status colors:

This one is a bit more tricky, but the principle is the same. All highlight groups of powerline start with Pl_. But some of them might not exist yet. For example, if you haven't entered visual mode yet, then the respective groups for visual mode won't be populated yet. So, enter insert, visual, and normal modes to populate the groups and then copy them. You can find them at the bottom of the output of the hl command. Then, paste them in your reload.vim and adapt them to be legit hl commands.

It may sound like a lot of work but it isn't. Here's a gist with the full reload.vim, and some gifs to guide you through.

like image 138
Paschalis Avatar answered Nov 07 '22 12:11

Paschalis


I had something very similar to this happening. I was able to solve it by making sure these were in the correct order:

syntax on
let g:solarized_termtrans=1
let g:solarized_termcolors=256
set background=dark
colorscheme solarized

I also used this for reloading

augroup reload_vimrc
autocmd!
autocmd BufWritePost $MYVIMRC source $MYVIMRC
augroup END

With those two I could do live updating of my vimrc without having to reload. I am using iTerm2 with Terminal vim. Hopefully this helps someone else out as I have spent quite a bit of time trying to get this live reloading working. Also make sure you have the newest versions of the solarized theme. I know it seems mundane to mention but it might make a difference.

like image 36
Cody Avatar answered Nov 07 '22 14:11

Cody


I guess that the highlightings are defined / changed by some plugin. The re-execution of :colorscheme resets those definitions. The plugins would have to hook into the ColorScheme event with an :autocmd, but most don't.

To work around this, try wrapping the :colorscheme in a guard:

if ! exists('g:colors_name') || g:colors_name !=# 'Tomorrow-Night-Eighties'
    colorscheme Tomorrow-Night-Eighties
endif
like image 23
Ingo Karkat Avatar answered Nov 07 '22 12:11

Ingo Karkat