0

I have a structure thus:

myproject/
    .vim/
    .vimrc

Contents of .vimrc are:

let g:usercolour = 0

if g:usercolour == 0
 set background=dark
 colorscheme default
endif
if g:usercolour == 1
 set background=light
 colorscheme desert
endif

"""""""Save Session Info""""""

function! MakeSession()
        exe "mksession! .vim/sessionub.vim"
endfunction

function! LoadSession()
      if (filereadable(".vim/sessionub.vim"))
        exe 'source .vim/sessionub.vim'
      else
        echo "No session loaded."
      endif
endfunction

if(argc() == 0)
    " Adding automatons for when entering or leaving Vim
    au VimEnter * nested :call LoadSession()
    au VimLeave * :call MakeSession()
endif

"""""End Save Session Info""""""""""

Essentially, I want to set g:usercolour=0 for a dark background and g:usercolour=1 for a light background. When I start vim with this .vimrc in my project folder (the .vimrc in the home directory is set to set exrc and set secure so that the project specific .vimrc is loaded), the background is set to light and not dark as I would have expected.

Is there a way to have vim correctly conditionally set the right background?

Image of the above linked here, reproduced below:

enter image description here


ETA1: As suggested, I tried flipping the lines thus:

if g:usercolour == 0
 set background=dark
 colorscheme default
endif

With the above, and everything else same as before, the case of usercolour=1 does not work. See image of that case here, reproduced below:

enter image description here


ETA2: As suggested, for desert colorscheme too, I set the background to light after setting the colorscheme. This does not resolve the issue either. See image here reproduced below:

enter image description here

Essentially, in this case, when I set g:usercolour = 1 the colorscheme continues to be default and the background dark. So, this if condition is never entered into.


ETA3: I tried to remove the parts of the vimrc that save and reload sessions. The situation even now continues to be quite confusing. See image here, reproduced below:

enter image description here

There are 3 stages in the image, one after the other. (The image is around 80 seconds long)

Stage 1 (first 25 or so seconds): with setting at 1, I save and close .vimrc. Then, I reenter vim. Colorscheme is default, but background is light. This should NOT be.

Stage 2 (next 25 seconds in the gif): Then, I make setting 0 in .vimrc. Save and exit. Then, I reenter vim. Colorscheme is default, and background is dark. This is correct.

Stage 3 (50 seconds until end of gif): Then, I make the setting 1 again, save and close .vimrc. Then, I reenter vim. Colorscheme is default, but background is light. This should NOT be.


ETA4: At the gentle and useful prodding of Maxim Kim, I figured out that the desert color scheme is not compatible with a light background! This link helped in my understanding. Hence, the above seemingly annoying discrepancies. It is resolved now!

2
  • 1
    what is your end goal? Do you know what background option means/does in vim?
    – Maxim Kim
    Sep 23, 2021 at 10:56
  • I tried different colorschemes with set background = light for some, and dark for others, and only some of these combinations worked for me. So, I wanted to set a listing of these pairs in the .vimrc so that I can choose the ones that suited me. However, it is clear that I do not understand what set background does. It seems quite complicated vi.stackexchange.com/questions/12104/…
    – Tryer
    Sep 23, 2021 at 11:03

1 Answer 1

1

When you run colorscheme default it resets background to default value which is in your case incorrectly detected to be light.

So I think if you change the order of commands to:

if g:usercolour == 0
 colorscheme default
 set background=dark
...

Then it should work for you.

5
  • The suggestion you have work in case usercolour == 0. When I set usercolor=1, the desert scheme is chosen but background persists to be dark and not light. Please see edit to my OP.
    – Tryer
    Sep 23, 2021 at 9:27
  • desert colorscheme sets background as dark, so your change before it doesn't have an effect.
    – Maxim Kim
    Sep 23, 2021 at 9:38
  • Ah. Is there a way to figure out which colorschemes change backgrounds? The reason why I ask is, I have papercolor theme also : github.com/NLKNguyen/papercolor-theme and according to this, atleast for this plugin, one should set the background before setting the colorscheme. See specific link github.com/NLKNguyen/papercolor-theme#-configure--vimrc
    – Tryer
    Sep 23, 2021 at 9:41
  • @Tryer depends on colorscheme implementation, probably by looking into the source of colorscheme
    – Maxim Kim
    Sep 23, 2021 at 9:42
  • The situation is even more complicated it seems. Please see ETA2 above. Essentially, when I change the setting from 0 to 1, for some otherworldly reason, the if condition corresponding to 1 is never entered into. I wonder if my saving the sessions info below has anything to do with it. Let me try removing those and see what is going on!
    – Tryer
    Sep 23, 2021 at 9:54

Your Answer

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

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