Switching themes automatically in Neovim

Article Image

We're going to be using Catppuccin and this setup is generally for MacOS but this idea will work on Linux too.

On macOS, you can run this shell command in Lua to get the current system appearance:

defaults read -g AppleInterfaceStyle 2>/dev/null

This will output:

  • "Dark" if dark mode is enabled.
  • Nothing if in light mode.

Now we can adjust our colorscheme.lua config by adding the following before we return the color scheme.

-- Detect system appearance (macOS only)
local handle = io.popen("defaults read -g AppleInterfaceStyle 2>/dev/null")
local result = handle:read("*a")
handle:close()

local is_dark = result:match("Dark") ~= nil

-- Decide flavour and background based on appearance
local flavour = is_dark and "macchiato" or "latte"
local background = {
  light = "latte",
  dark = "mocha",
}

To complete your colorscheme config it should look like the below.

-- Detect system appearance (macOS only)
local handle = io.popen("defaults read -g AppleInterfaceStyle 2>/dev/null")
local result = handle:read("*a")
handle:close()

local is_dark = result:match("Dark") ~= nil

-- Decide flavour and background based on appearance
local flavour = is_dark and "macchiato" or "latte"
local background = {
  light = "latte",
  dark = "mocha",
}

return {
  {
    "catppuccin/nvim",
    name = "catppuccin",
    lazy = false,

    opts = {
      flavour = flavour, -- use detected flavour
      background = background,
      transparent_background = false,
      no_italic = true,
      no_bold = true,
      no_underline = true,
      term_colors = true,

      integrations = {
        cmp = true,
        gitsigns = true,
        neotree = true,
        treesitter = true,
        notify = false,
        mini = {
          enabled = true,
          indentscope_color = "",
        },
      },
    },
  },

  {
    "LazyVim/LazyVim",
    opts = {
      colorscheme = "catppuccin",
    },
  },
}

This is for the Catppuccin theme. Which I have every where, you're able to amend the above Catppuccin options by following this guide