reloading vimrc causes different syntax highlighting - 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?

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)
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):
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:
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.

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.

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

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

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.

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.

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.

set syntax enable after colorscheme

Related

not able to change highlight group setting in .vimrc

I am trying to change the spellcheck highlight group. So in the end of my .vimrc
I add the following code
highlight clear SpellBad
highlight SpellBad cterm=underline
when I open a new file, it is still showing the old syntax highlighting. But if I run the same commands inside vim manually after opened the file, it will work as expected.
Any idea what is going wrong here? Thanks!
Tweaks to a colorscheme have to happen after the colorscheme has been set. Usually, if you have the :colorscheme in your ~/.vimrc, and put the :highlight commands after it, that should work.
Your case seems to be different (which could be caused by a plugin manager affecting the loading order, or you might even have a dynamically changing colorscheme). To handle such eventualities, you can instead hook into the ColorScheme event:
autocmd ColorScheme * highlight clear SpellBad
autocmd ColorScheme * highlight SpellBad cterm=underline

Need to source .vimrc from within vim to achieve wanted effect (italic comments)

Following this great answer when trying to have italic comments in vim, I encounter a strange behaviour: I have those three relevant lines inside my .vimrc:
set t_ZH=^[[3m
set t_ZR=^[[23m
highlight Comment cterm=italic
When I open a file with vim, comments are not displayed in italic. However, if I do :highlight Comment cterm=italic or even :source ~/.vimrc, I get italic comments. Any idea for possible reasons for this?
EDIT: Following the helpful answers, I solved the problem by replacing highlight Comment cterm=italic in my .vimrc with autocmd VimEnter * :highlight Comment cterm=italic.
This article about scripting vim was also helpful.
It does not have to be a user installed plugin which overwrittes your setting.
It could also be a filetype plugin which comes with vim. These filetype specific things are loaded after your .vimrc so they overwrite your settings there. Sourcing your .vimrc afterwards will overwrite them again.
Enter the following command to see where your problem lies:
:verbose hi Comment

Vim Vertical Split Highlight Not Turning Off

I am having trouble on making the Vim Vertical Split Highlight disappear permanently.
When I use this command in Vim command line :hi VertSplit ctermbg=None the vertical split highlight disappears (as seen in Screen 'B'), but when I use the same command in the .vimrc.after (I am using janus for vim, as seen in screen 'A') the command does not work. I have tried many variations of the command with 'let','set', etc but no result, the Vertical Split stays.
Can someone please help and let me know what I doing wrong or what I am suppose to do? I want to remove the VerticalSplit highlight from my Vim.
Your .vimrc is executed sequentially, so probably this command is
being executed but then having its changes overwritten by another
command or plugin of yours. The solution would then be to move the
command to after these other changes happen. This may and may not work
depending on your setup (check :h startup for a list of what happens
at startup and in what order).
The best solution in my opinion is to use an auto command. These will
execute a command after an event, for example startup finished or
colorscheme changed. Add this to your .vimrc:
autocmd! ColorScheme * hi VertSplit ctermbg=none
Now, whenever the colorscheme is changed, this command will trigger for
any file (*). This will trigger as well by your initial colorscheme
set in your .vimrc.
Important! If your colorscheme set in your .vimrc is done before
your autocmd line, then of course nothing will be triggered (it won't
be capturing the event yet). In this case you have two options: first is
to obviously move the colorscheme set to after the autocmd. Second
option is add an additional event listener by changing the line to:
autocmd! VimEnter,ColorScheme * hi VertSplit ctermbg=none
Now this will be triggered both after Vim's startup process (after all
.vimrc stuff is done and loaded) and also whenever you change your
colorscheme. This way, you don't need to change your colorscheme files.
And you should never have to do that anyway. The point of .vimrc is to
keep all your configuration in one file. So this editor of course
provides options to make this happen.

Vim syntax theme; different behavior from .vimrc/colo call

I'd like to load a vim syntax theme from .vimrc.
The strange thing is that the theme looks different when loaded from .vimrc as it does when called from within vim with colo molokai_dark.
The .vim file doesn't have if has("gui_running") or t_Co (only in the 256 color terminal support-section) like mentioned here.
Why is that the case and how can I control this behavior?
thx in advance!
You don't mention / show the actual changes, nor your actual configuration, and the colorscheme itself looks good, so I can only offer a workaround:
You can emulate the "from within Vim" via the following autocmd in your ~/.vimrc:
autocmd VimEnter * colorscheme molokai_dark

Why does VIM highlight some words?

I noticed that with different colorschemes VIM is underlining/highlighting some words. Why is this and how to turn it off ?
with another colorscheme
I'm using spf13-vim configuration and connecting remotely with Putty.
VIM is correctly assuming this file to be a python file (:set filetype returns "python")
It seems like your Vim is doing spell-checking for you. You can turn this off by adding
set nospell
in your .vimrc file. To turn it back on in a file, you can do:
:setlocal spell spelllang=en_us
for spell-checking with American English. :setlocal changes settings for current buffer, while :set makes the changes for all currently open buffers. You can read more about how spell-checking with Vim works here.
It may be useful for you to automatically enable spell checking for certain files. For example, to enable spell checking in .tex files, you can add the following to your .vimrc:
" Enable spell checking when opening .tex files
autocmd!
au BufNewFile,BufRead *.tex setlocal spell spelllang=en_us
" Or if you have filetype detection enabled:
" au FileType tex setlocal spell spelllang=en_us
Note that autocmd! clears the previously defined au commands and is needed only once.
Most filetypes (like python) in Vim come with a syntax that defines highlight groups (see them via :highlight). A colorscheme then provides combinations of foreground / background color and/or formatting like bold and italic, for terminals, color terminals, and/or GVIM.
Choose a colorscheme that you find visually appealing; some come with Vim, many more can be found on the Internet, most at http://www.vim.org/.
If you're just annoyed by one or two minor things in a particular colorscheme, you can modify the items via the :highlight command. To disable a highlighting, use, e.g.
:highlight clear Statement
or (when the group is linked to another group, effectively inheriting its appearance)
:highlight link Statement NONE
(These must be issued after the :colorscheme command that sets your preference.)
I have came across two kinds of highlight that I don't like.
1.indent highlight and tab arrow reminder, you can solve it by adding
let g:indent_guides_enable_on_vim_startup = 0
set nolist
to ~/.vimrc.local
2.highlight the usual words, like Chinese words and wrong spell words, you can solve it by adding
set nospell
to ~/.vimrc.local

Resources