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:
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:
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:
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:
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!
background
option means/does in vim?