pvinis
pvinis

Reputation: 4149

reloading vimrc causes different syntax highlighting

When I open up a file in macvim it is like this https://i.sstatic.net/cCxmo.jpg. I have set ,V to :source ~/.vimrc<CR>.

After I have this file open, I press ,V, and the syntax highlighting changes to this https://i.sstatic.net/Tn9Ia.jpg. 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?

Upvotes: 7

Views: 2682

Answers (8)

Goldman
Goldman

Reputation: 1

set syntax enable after colorscheme

Upvotes: 0

user2429703
user2429703

Reputation: 11

autocmd BufWritePost *vimrc,*exrc :call feedkeys(":source %\<cr>")

Fixed it right away for me. Must be some weird issue with the ordering. I also have a keystroke to resource the file and this always works even when the autocmd was failing.

Upvotes: 1

Rastapopoulos
Rastapopoulos

Reputation: 517

I have absolutely no idea why, (probably for the same reason that doing :source ~/.vimrc manually works) but for me replacing

autocmd BufWritePost *vimrc,*exrc :source %

with

autocmd BufWritePost *vimrc,*exrc :call feedkeys(":source %\<cr>")

fixed the problem.

Upvotes: 1

Paschalis
Paschalis

Reputation: 12321

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:

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.

Upvotes: 5

Cody
Cody

Reputation: 608

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.

Upvotes: 3

Timoth&#233;e Boucher
Timoth&#233;e Boucher

Reputation: 1565

Most likely unrelated to your particular case, but I had a similar problem so I thought I'd share since it's the first StackOverflow result on Google.

My issue with reloading was in two phases: using the dark Solarized theme, reloading .vimrc would first change the colors slightly, then, reloading a second time, it would switch to the light Solarized theme.

The lines about colors in my .vimrc were:

set background="dark"
let g:solarize_termcolors=256
colorscheme solarized

The problem? The first line shouldn't have quotes:

set background=dark

I can now reload .vimrc without changing the colors. I'm not sure why it would work one time and work a different way after though. I realized that after commenting out everything but these lines as others have suggested.

Upvotes: 0

Pencilcheck
Pencilcheck

Reputation: 2942

This works for me

"auto reload vimrc once changed
if has("autocmd")
  autocmd! BufWritePost .vimrc source $MYVIMRC

  " This fixes the color changes and things not working :D
  autocmd! BufWritePost .vimrc filetype plugin indent on
endif

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172778

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

Upvotes: 1

Related Questions