Skip to content

How to define a keymap to toggle btw dark and light modes? #426

Answered by kkafar
hopezh asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @hopezh, I've managed to get the toggler working with following code:

  1. Tokyonight configuration
require("tokyonight").setup({
    style = 'moon',
    light_style = 'day',
})

-- default value
vim.opt.background = "dark" 
  1. Theme toggler
local function toggle_theme()
  if vim.opt.background:get() == "dark" then
    vim.opt.background = "light"
    -- In my case it was unnecessary to set theme directly,
    -- as tokyonight reacted to change of vim.opt.background
    -- vim.cmd("colorscheme tokyonight-day")
  else
    vim.opt.background = "dark"
    -- vim.cmd("colorscheme tokyonight-moon")
  end
end

vim.keymap.set("n", "<leader>ut", toggle_theme, {})

The difference from previous snip…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@hopezh
Comment options

@hopezh
Comment options

@kkafar
Comment options

Answer selected by hopezh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants