0

There are probably 20 better ways to do what I'm trying to do. Please direct me to them if so - I haven't found them and not quite sure what to terms to search.

I would prefer to set background=dark or background=light depending on what my system theme is, but I haven't been able to figure that out and got tired of trying.

So instead I'm trying something else, that's not quite as good as that but I think will be an improvement over manually toggling background every time I open vim: when I run a custom command to toggle the background, also set the some register equal to "dark" or "light". Then, when Vim starts up, check that register and set the background accordingly.

However, Vim doesn't seem to actually check the contents of the registers on start up, or at least how I'm doing it.

Any advice? Is there some other way to set the value of a variable from within Vim and have that persist after Vim is exited?

Here's the background toggle function:

function! ToggleBackground()
    if (&background ==# "dark")
        set background=light
        let @b = 'light'  " set the b register = 'light'
    else
        set background=dark
        let @b = 'dark'  " set the b register = 'dark'
    endif
endfunction

And here's how I'm trying to set background at startup:

if  @b == "dark"
    set background=dark
else
    set background=light
endif

The if statement never evaluates to true when starting Vim, even though I've checked the value of the register using (:echo @b) after running the toggle command, and it is indeed being set to 'dark'.

1

2 Answers 2

0

Alternatively, execute :rviminfo (or :rshada for Neovim) before checking your register contents to make sure the file was read by this point. This is perfectly safe as Vim checks if the data was read in and refuses to do this twice (unless bang, e.g. :rviminfo!).

rviminfo
let &bg = @b
1
  • Thanks for this answer too, it seems to work better.
    – kdwarn
    Mar 24, 2021 at 5:30
0

vim stores the values of registers in the viminfo file (neovim shada). Loading this file is one of the last initialization steps, long after sourcing the vimrc (neovim init.vim).

A work around is to use the VimEnter autocmd:

function SetColors()
  if @b == "dark"
    set background=dark
  else
    set background=light
  endif
endfunction

augroup vimrc_colors
    au!
    autocmd VimEnter * call SetColors()
augroup END

This will allow the register's value to be prior to lookup. Although if your vimrc is very complex, there is a chance you might see a brief "flash of unstyled vim" during startup.

3
  • Perfect, thanks!
    – kdwarn
    Mar 23, 2021 at 1:06
  • For some reason, doing it this way - as opposed to the answer from @matt - stopped the tagbar (via the vim-airline extension) from highlighting the current buffer when vim is first opened. It gets highlighted when I toggle back and forth, but for that reason I ended up going with their solution.
    – kdwarn
    Mar 24, 2021 at 5:33
  • For others who don't want to use rviminfo, a possible workaround is to add AirlineRefresh to the end of the SetColors function. @kdwarn
    – Mass
    Mar 24, 2021 at 20:47

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.