0

I have a shortcut to set the background light, dark, or transparent. I want to use a different color scheme for when it is dark or light.

How would I go about implementing it?

I don't know programming and this is my failed try.

if vim.cmd("set background") == "background=light" then
    vim.cmd("colorscheme modus-operandi")
end

I think I need something like this, but in lua.

Here is my config.

1 Answer 1

1

I would do:

if vim.o.background == 'dark' then
    print("dark")
    vim.cmd("colorscheme modus-vivendi")
    -- vim.cmd("colorscheme dracula")
else
    print('light')
    vim.cmd("colorscheme modus-operandi")
    -- vim.cmd("colorscheme gruvbox")
end

vim.api.nvim_create_autocmd({"OptionSet"}, {
    pattern = {"background"},
    callback = function(ev)
        if vim.o.background == 'dark' then
            print('late dark')
            vim.cmd("colorscheme modus-vivendi")
            -- vim.cmd("colorscheme dracula")
        else
            print('late light')
            vim.cmd("colorscheme modus-operandi")
            -- vim.cmd("colorscheme gruvbox")
        end
        -- force a full redraw:
        vim.cmd("mode")
    end
})
10
  • 1
    Just tried it, does not work.
    – enrico223
    Commented Apr 6, 2023 at 9:57
  • Are you sure it is executed? Is the background set when it is executed or it set after? Maybe it need to be part of an autocmd. Commented Apr 6, 2023 at 10:20
  • 1
    Surely there's a better way to get the option value… maybe vim.o.bg or something? Anyway, see :help OptionSet
    – D. Ben Knoble
    Commented Apr 6, 2023 at 12:35
  • 1
    I put it in init.lua, and when I start nvim, I get echo &background as output in the "command section" (the one under the status line). So I am fairly confident it is being executed.
    – enrico223
    Commented Apr 6, 2023 at 13:11
  • But when it is executed, is the colorscheme already loaded (if modus-operandi is a plugin is the plugin already declared)? Is the code before are after the Plug command? Commented Apr 6, 2023 at 13:15

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.