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 
       set background=light 

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.


3 Answers 3


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"
       set background=light
       let s:mybg = "light"
    " set background=light
    colorscheme solarized

nnoremap <silent> <leader>sz :call BgToggleSol()<cr>
  • Hmmm. What does :verbose set background report?
    – Rich
    Jan 15, 2016 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! Jan 15, 2016 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? Jan 15, 2016 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. Jan 15, 2016 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. Jan 15, 2016 at 14:08

You might want something like:

map <Leader>bg :let &background = ( &background == "dark"? "light" : "dark" )<CR>

This toggles between dark and light. Leader's default, if not set otherwise, is \.

Btw you can also do so with colorschemes:

map <Leader>cs :execute ( g:colors_name == "selenized_bw" ? "colorscheme selenized" : "colorscheme selenized_bw" ) <CR>

Or together:

noremap <Leader>bg :let &background = ( &background == "dark" ? "light" : "dark" )<CR> <bar> :execute ( g:colors_name == "selenized_bw" ? "colorscheme selenized" : "colorscheme selenized_bw" ) <CR>

Here's an alternative two parter answer, that toggles both the colorscheme and the background. First, using your mapping, then a specific mapping pattern I use to mirror another popular plugin with many provided toggles.

This mapping toggles both the background to light or dark, and the colorscheme, using printf.

  1. I'll use your <leader>sz mapping here:

    nnoremap <silent><expr><leader>sz printf(":set bg=%s \| colo %s\r", &bg ==# 'dark' ? 'light' : 'dark', &bg ==# 'dark' ? 'modest' : 'moonfly')

Note that this uses <expr> in addition to your <silent> and this is required since we're using printf. Read :help <expr>

  1. I use tpope's vim-unpaired which advertises itself as "Pairs of handy bracket mappings" but it also provides many toggles that use a yo_ mapping.

    So I set my colorscheme toggle to use the same pattern:

    nnoremap <silent><expr>yot printf(":set bg=%s \| colo %s\r", &bg ==# 'dark' ? 'light' : 'dark', &bg ==# 'dark' ? 'modest' : 'moonfly')

    For some other examples of toggles, because why not, here's the list from vim-unimpaired and note that I am overwriting their colorcolumn toggle because I didn't realize it when I started using yot and evidently I never toggle that column:

    On  Off Toggle  Option
    *[ob*   *]ob*   *yob*   'background' (dark is off, light is on)
    *[oc*   *]oc*   *yoc*   'cursorline'
    *[od*   *]od*   *yod*   'diff' (actually |:diffthis| / |:diffoff|)
    *[oh*   *]oh*   *yoh*   'hlsearch'
    *[oi*   *]oi*   *yoi*   'ignorecase'
    *[ol*   *]ol*   *yol*   'list'
    *[on*   *]on*   *yon*   'number'
    *[or*   *]or*   *yor*   'relativenumber'
    *[os*   *]os*   *yos*   'spell'
    *[ot*   *]ot*   *yot*   'colorcolumn' ("+1" or last used value)
    *[ou*   *]ou*   *you*   'cursorcolumn'
    *[ov*   *]ov*   *yov*   'virtualedit'
    *[ow*   *]ow*   *yow*   'wrap'
    *[ox*   *]ox*   *yox*   'cursorline' 'cursorcolumn' (x as in crosshairs)

    tpope comments on why they chose y: "The mnemonic for y is that if you tilt it a bit it looks like a switch."


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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