Tutorial :How can I stop all the colors being destroyed when I change hi Normal ctermbg



Question:

au InsertEnter * hi Normal ctermbg=233  

Doing this causes all the colors from my colorscheme (elflord) to go away and change into defaults. What can I do to stop this or work around it somehow?


Solution:1

The code for handling this in CSApprox is:

" colors_name must be unset and reset, or vim will helpfully reload the  " colorscheme when we set the background for the Normal group.  " See the help entries ':hi-normal-cterm' and 'g:colors_name'  if exists("g:colors_name")    let colors_name = g:colors_name    unlet g:colors_name  endif    " Similarly, the global variable "syntax_cmd" must be set to something vim  " doesn't recognize, lest vim helpfully switch all colors back to the  " default whenever the Normal group is changed (in syncolor.vim)...  if exists("g:syntax_cmd")    let syntax_cmd = g:syntax_cmd  endif  let g:syntax_cmd = "PLEASE DON'T CHANGE ANY COLORS!!!"    " ... change normal here ...    if exists("colors_name")    let g:colors_name = colors_name  endif    unlet g:syntax_cmd  if exists("syntax_cmd")    let g:syntax_cmd = syntax_cmd  endif  


Solution:2

And finally...

" Makes it VERY obvious if you are in insert mode or not :)  if version >= 700        function EnterPastel()          redir => current | silent highlight Normal | redir END            let current = substitute(current, " xxx ","  ", "")          " Weird junk char at start          let current = matchstr(current, '\(Normal.*\)')          redir => background | silent set background | redir END          let background = matchstr(background, '\(background=.*\)')          let s:highlight_normal = current          let args = split(current, "")          call filter(args, 'v:val !~ "ctermbg"')          let pastel_normal = 'highlight '.join(args).' ctermbg=233'            if exists("g:colors_name")              let colors_name = g:colors_name              unlet g:colors_name          endif            if exists("g:syntax_cmd")              let syntax_cmd = g:syntax_cmd          endif          let g:syntax_cmd = "Who you lookin at kid?"            exec pastel_normal          exec 'set '.background            if exists("colors_name")              let g:colors_name = colors_name          endif            unlet g:syntax_cmd          if exists("syntax_cmd")              let g:syntax_cmd = syntax_cmd          endif       endfunction        function LeavePastel()          redir => background | silent set background | redir END          let background = matchstr(background, '\(background=.*\)')          if exists("g:colors_name")              let colors_name = g:colors_name              unlet g:colors_name          endif            if exists("g:syntax_cmd")              let syntax_cmd = g:syntax_cmd          endif          let g:syntax_cmd = "Who you lookin at kid?"            highlight clear Normal            if s:highlight_normal !~ "ctermbg="              " Thanks godlygeek for this one              let s:highlight_normal = s:highlight_normal." ctermbg=NONE"          endif            exec 'highlight '.s:highlight_normal          exec 'set '.background            if exists("colors_name")              let g:colors_name = colors_name          endif            unlet g:syntax_cmd          if exists("syntax_cmd")              let g:syntax_cmd = syntax_cmd          endif       endfunction    au InsertEnter * hi StatusLine term=reverse ctermfg=DarkRed ctermbg=7 guibg=black  au InsertLeave * hi StatusLine term=reverse ctermfg=7 ctermbg=0 guibg=black  au InsertEnter * call EnterPastel()  au InsertLeave * call LeavePastel()    endif  


Solution:3

There are actually a lot more issues involved with colors than just the ones here, if you want to see them all, I rolled the whole lot into a plugin, which is at:

http://www.vim.org/scripts/script.php?script_id=3165

And works with gvim -v, normal vim and gvim in GUI mode.

Here's the current version:

" You can change the colours to ones you like here  let s:pastel_guibg = '#121212'  let s:pastel_ctermbg = 233    " +++ Make it obvious which mode we are in  set laststatus=2 " always show status line    " Makes it VERY obvious if you are in insert mode or not :)  if version >= 700        function EnterPastel()          redir => current | silent highlight Normal | redir END            let current = substitute(current, " xxx ","  ", "")          " Weird junk char at start          let current = matchstr(current, '\(Normal.*\)')          redir => background | silent set background | redir END          let background = matchstr(background, '\(background=.*\)')          let s:highlight_normal = current            " As pointed out by Dave Kirby, gvim puts font info into there which          " breaks things. This also revealed a lot of other things to me which          " aren't working with gvim, i use it in -v mode and didn't realise so          " I have tried to make it more GUI user friendly.          let s:gfn = matchstr(current,'\font=\(.*\)$',"","")            let args = split(current, "")          call filter(args, 'v:val !~ "guibg"')          call filter(args, 'v:val !~ "ctermbg"')          let pastel_normal = 'highlight '.join(args).' ctermbg='.s:pastel_ctermbgi.' guibg='.s:pastel_guibg             if exists("g:colors_name")              let colors_name = g:colors_name              unlet g:colors_name          endif            if exists("g:syntax_cmd")              let syntax_cmd = g:syntax_cmd          endif          let g:syntax_cmd = "Who you lookin at kid?"            exec pastel_normal          exec 'set gfn='s:gfn          exec 'set '.background            if exists("colors_name")              let g:colors_name = colors_name          endif            unlet g:syntax_cmd          if exists("syntax_cmd")              let g:syntax_cmd = syntax_cmd          endif       endfunction        function LeavePastel()          redir => background | silent set background | redir END          let background = matchstr(background, '\(background=.*\)')          if exists("g:colors_name")              let colors_name = g:colors_name              unlet g:colors_name          endif            if exists("g:syntax_cmd")              let syntax_cmd = g:syntax_cmd          endif          let g:syntax_cmd = "Who you lookin at kid?"            highlight clear Normal            if s:highlight_normal !~ "ctermbg="              " Thanks godlygeek for this one              let s:highlight_normal = s:highlight_normal." ctermbg=NONE"          endif            if s:highlight_normal !~ "guibg"              let s:highlight_normal = s:highlight_normal." guibg=NONE"          endif            exec 'highlight '.s:highlight_normal          exec 'set gfn='s:gfn          exec 'set '.background            if exists("colors_name")              let g:colors_name = colors_name          endif            unlet g:syntax_cmd          if exists("syntax_cmd")              let g:syntax_cmd = syntax_cmd          endif       endfunction    au InsertEnter * hi StatusLine term=reverse ctermfg=DarkRed ctermbg=7 guibg=black  au InsertLeave * hi StatusLine term=reverse ctermfg=7 ctermbg=0 guibg=black  au InsertEnter * call EnterPastel()  au InsertLeave * call LeavePastel()    endif  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »