A great question!
Yes, you can definitely set Neovim's color scheme based on the system's light or dark mode automatically in your init.lua. Here's how:
Step 1: Detect system theme (light/dark mode)
We'll use the vim.fn module to execute a shell command that detects the system theme. The command varies depending on your OS:
- macOS:
defaults read -g AppleInterfaceStyle (returns "Dark" or throws an error if light mode)
- Linux ( GNOME ):
gsettings get org.gnome.desktop.interface color-scheme (returns "prefer-dark" or "prefer-light")
- Linux ( KDE ):
kreadconfig5 --group Colors --key ColorScheme (returns a string containing "BreezeDark" or "BreezeLight")
- Windows: We'll use PowerShell:
(Get-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme).AppsUseLightTheme (returns 0 for dark mode or 1 for light mode)
Let's create a Lua function to detect the system theme:
local function get_system_theme()
local os_name = vim.loop.os_uname().sysname
if os_name == "Darwin" then -- macOS
local handle = io.popen("defaults read -g AppleInterfaceStyle 2>&1")
if handle then
local result = handle:read("*a")
handle:close()
return result:match("Dark") and "dark" or "light"
end
elseif os_name == "Linux" then
-- Try GNOME first
local handle = io.popen("gsettings get org.gnome.desktop.interface color-scheme 2>&1")
if handle then
local result = handle:read("*a")
handle:close()
if result:match("prefer%-dark") then return "dark" end
if result:match("prefer%-light") then return "light" end
end
-- Try KDE
local handle = io.popen("kreadconfig5 --group Colors --key ColorScheme 2>&1")
if handle then
local result = handle:read("*a")
handle:close()
return result:match("Dark") and "dark" or "light"
end
elseif os_name == "Windows_NT" then -- Windows
local handle = io.popen("powershell -Command \"(Get-ItemProperty -Path HKCU:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize -Name AppsUseLightTheme).AppsUseLightTheme\" 2>&1")
if handle then
local result = handle:read("*a")
handle:close()
return tonumber(result) == 0 and "dark" or "light"
end
end
-- Default to light if detection fails
return "light"
end
Step 2: Set Neovim color scheme based on system theme
Now, let's define your desired color schemes for light and dark modes. I'll use vim.cmd to set the colorscheme, as it's the most straightforward way.
Add this to your init.lua:
local function set_colorscheme()
local theme = get_system_theme()
if theme == "dark" then
vim.cmd [[colorscheme onedark]] -- your dark mode colorscheme
else
vim.cmd [[colorscheme onelight]] -- your light mode colorscheme
end
end
-- Call it once to set the initial colorscheme
set_colorscheme()
Step 3: Auto-update colorscheme when system theme changes
To automatically update Neovim's colorscheme when the system theme changes, we'll use vim.api.nvim_create_autocmd (Neovim 0.7+) to listen for the FocusGained event, which is triggered when Neovim regains focus (e.g., when you switch back to Neovim after changing the system theme).
Add this to your init.lua:
vim.api.nvim_create_autocmd("FocusGained", {
pattern = "*",
command = "lua set_colorscheme()",
})
This autocmd will re-run set_colorscheme() whenever Neovim gains focus, effectively updating the colorscheme if the system theme has changed.
Putting it all together
Here's the complete code snippet for your init.lua:
local function get_system_theme()
-- ... (same implementation as above)
end
local function set_colorscheme()
local theme = get_system_theme()
if theme == "dark" then
vim.cmd [[colorscheme onedark]] -- your dark mode colorscheme
else
vim.cmd [[colorscheme onelight]] -- your light mode colorscheme
end
end
set_colorscheme() -- Initial setup
vim.api.nvim_create_autocmd("FocusGained", {
pattern = "*",
command = "lua set_colorscheme()",
})
Tips
- Make sure to replace
onedark and onelight with your actual color scheme names.
- If you're using an older Neovim version (< 0.7), use
vim.api.nvim_exec instead of vim.api.nvim_create_autocmd.
- The
get_system_theme() function might not work perfectly on all Linux distributions or versions. You may need to tweak the command or add more cases.
- If you experience performance issues or flickering when switching themes, consider adding a small delay before updating the colorscheme (e.g., using
vim.defer_fn).
Enjoy your auto-switching Neovim color scheme!