I didn't put color xxx in my .vimrc. So I see this when I first open my .vimrc.

:color gives me default here.

Then I type :color darcula. And it changes to this.

:color gives me darcula here (naturally).

Then I type :set background=dark. And it changes to this.

:color gives me default here (amazingly).

So my question is, what exactly does :set background=dark do?

I tried :h background and didn't find anything helpful. I understand that this command doesn't actually change the background, only vim will change colors accordingly. But I still want to ask this question. Because I'm really happy with the color after :color darcula and :set background=dark so I wonder how to write my own color scheme without having to put set background=dark in my .vimrc.

I took advice from @statox and put set background=dark at the last line of my darcula.vim. Then color darcula gives me the third screenshot. However when I put it at the first line, color darcula gives me the second screenshot. Also set bg=dark changes it to the third screenshot.

I assumed by putting it at the first line, I'm telling vim that this colorscheme is designed for a dark background. By putting it at the last line, on the contrast, I let vim assume that this colorscheme is designed for a light background and then make vim change some of it by itself (when it processes the last line).

If what I assume happened is true, my question is still unanswered. What exactly did vim change, when I use set bg=dark AFTER it has loaded a light colorscheme?

@muru Greetings! I had read this question before asking mine. It is mentioned in that post that :set bg=light and :set bg=dark are not opposite, but what exactly they are is not explained. Also that post is why I want to avoid :set background=dark in my .vimrc.
– Michael MaApr 17 '17 at 8:57

2 Answers
2

What happens when you :set background

The basic mechanism by which :set background works is actually pretty simple, and
it is documented (although not particularly clearly) in :help 'background'.

When 'background' is set Vim will adjust the default color groups for
the new value. But the colors used for syntax highlighting will not
change. g:colors_name
When a color scheme is loaded (the "g:colors_name" variable is set)
setting 'background' will cause the color scheme to be reloaded. If
the color scheme adjusts to the value of 'background' this will work.
However, if the color scheme sets 'background' itself the effect may
be undone.

So what actually happens when the 'background' option is changed is this:

If no colour scheme is currently loaded

Vim reloads the default colours, using foreground colours deemed appropriate for
the new setting of 'background'

If a colour scheme is currently loaded

Vim reloads the colour scheme. When the colour scheme is loaded, either the
colour scheme sets colours appropriate to the new setting of 'background', or
the colour scheme resets'background' to the old value.

As you have already described, the 'background' option is not intended to set
the background, but instead to inform Vim whether the background is dark or
light.

So if the colour scheme resets 'background', Vim takes this to mean
that the colour scheme simply doesn't support the new setting of 'background'.
Since the user has indicated they are using such a background this means that
the current colour scheme is no longer appropriate, and so Vim unsets
g:colors_name and then reloads the default colours.

If you'd like to explore the actual code that is run when 'background' is set, you can start here.

What's happening in your installation

The odd behaviour you describe (in the first version of your question) can be
explained by this line in the documentation quoted above:

When a color scheme is loaded (the "g:colors_name" variable is set)

The problem is that your colors/darcula.vim file doesn't set this value, and
so even though the colors changed when you invoked :color darcula, Vim
doesn't consider a colour scheme to currently be loaded.

Therefore, when you call :set background=dark Vim carries out the step in the
"If no colour scheme is currently loaded" section above, and sets the default
colours for a dark background. This explains why :color then reports
default.

(Note, however, that setting the default colours does not, as you might expect,
remove all the colours that had previously been set, and this is why you end up
with the results you see, rather than the colours that are displayed when you
start up Vim in the first place. There's some discussion of this issue
here)

In short, your colour scheme is misconfigured: See :edit
$VIMRUNTIME/colors/README.txt (in particular the section at the bottom) for
more details on this, and on the interaction between 'background' and colour
schemes.

Thank you so much! I feel completely understood when you say it "does not, as you might expect, remove all the colours that had previously been set". I will surely go read the discussions!
– Michael MaAug 7 '17 at 2:36

When set to "dark", Vim will try to use colors that look good on a
dark background. When set to "light", Vim will try to use colors that
look good on a light background. Any other value is illegal.

Your colorscheme defines 2 groups of colors: the ones used on a light background
and the ones used on a dark background. The background option will tell vim
which group of colors defined by your colorscheme should be used.

Setting this option does not change the background color, it tells Vim
what the background color looks like. For changing the background
color, see :h :hi-normal.

When 'background' is set Vim will adjust the default color groups for
the new value. But the colors used for syntax highlighting will not
change.

That's the follow up of the previous paragraph: It's the colorscheme which
defines the groups of colors and background selects one of these groups.

Finally if you want to write your own color scheme without having to put set background=dark in your vimrc, remember that a colorscheme is nothing more than a program in vimscript, so nothing stops you from using the command directly in the code of your colorscheme like e.g. jellybeans or apprentice

Thank you! I put set background=dark in darcula.vim and produced the same effect as when I put it in .vimrc. But I'm still curious, how exactly does Vim "try to use colors that look good on a dark background"? Is there a mapping or something like that defined somewhere?
– Michael MaApr 18 '17 at 2:43