3

Both background and colorscheme in ~/.vimrc are badly misbehaving!

I have setup solarized to work on vim and it can manually toggle between background light/dark. My general settings in ~/.vimrc are:

Plug 'altercation/vim-colors-solarized' "I use plug.vim
syntax enable
set background=light
set t_Co=256
let g:solarized_termcolors=256

Operating system: Ubuntu 14.04.; xterm and works well with 256 colors.

I'm trying to map in ~/.vimrc background toggling. For this purpose :help solarized (in vim) suggests to use:

call togglebg#map("<F9>")

but this doesn't work because it changes the colorscheme from solarized to default (my intention was to toggle only in solarized between dark and light).

So I remove from ~/.vimrc call togglebg#map("<F9>") and replaced it with what most google/superuser/vim.wikia commentators, in various flavors, have suggested with the following:

function! BgToggleSol()
    if (&background == "light")
      set background=dark 
    else
       set background=light 
    endif
endfunction

nnoremap <silent> <leader>sz :call BgToggleSol()<cr>

colorscheme solarized

But this has the same problem (that toggling resets colorscheme to default). While if colorscheme solarized is executed directly after set background=... in the if else endif clause then the background stops toggling between dark/light.

I must have exhausted nearly all possibilities, but the problem is still not going away.

Any suggestion is very welcome. Thanks in advance.

1

Something is definitely not right. I have in my .vimrc something like this:

set background=dark
colorscheme solarized

And when I do :set bg? after that, it shows "light". I think that's the reason the if/else/endif in you function doesn't work after 'colorsheme solarized'.

The only workaround I can think of is to maintain the state in a separate variable:

" Initially set it to "dark" or "light" according to your default
let s:mybg = "dark"
function! BgToggleSol()
    if (s:mybg ==? "light")
       set background=dark
       let s:mybg = "dark"
    else
       set background=light
       let s:mybg = "light"
    endif
    " set background=light
    colorscheme solarized
endfunction

nnoremap <silent> <leader>sz :call BgToggleSol()<cr>
  • Hmmm. What does :verbose set background report? – Rich Jan 15 '16 at 10:58
  • Thanks Boris Serebrov! It certainly is working. It's really nice and it's making so much difference in quality of work! – Annis Monadjem Jan 15 '16 at 13:46
  • There is still a small nuance that's bugging me. The mapping togs well between dark/light using the script you drop earlier (now in my ~/.vimrc). The problem now (really a small issue) is that when I manually execute :set background? does not give always the right answer: it might be really a light screen and the cmdline reports dark and vice versa. Any suggestion why that might be and how to fix it please? – Annis Monadjem Jan 15 '16 at 13:54
  • @Rich the :verbose set background says Last set from ~/.vim/plugged/vim-colors-solarized/colors/solarized.vim and this also confirms that calling colorscheme solarized also changes the background. – Boris Serebrov Jan 15 '16 at 14:06
  • 1
    @AnnisMonadjem my solution is a workaround to the problem of background being set incorrectly. I would also prefer it to have correct value and then your initial code would just work and additional mybg variable would be not needed. I think you can try to report this issue to the vim-colors-solarized developers, maybe they will be able to fix it. – Boris Serebrov Jan 15 '16 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.