local theme_cache_path = vim.fn.stdpath("data") .. "/last_theme.txt" local is_programmatic_change = false local function load_theme() local theme = nil local f = io.open(theme_cache_path, "r") if f then theme = f:read("*l") f:close() end if theme then theme = theme:match("^%s*(.-)%s*$") -- Trim any whitespace end is_programmatic_change = true local status = pcall(vim.cmd.colorscheme, theme) if not status then vim.cmd.colorscheme("habamax") end end local group = vim.api.nvim_create_augroup("ThemePersistance", { clear = true }) vim.api.nvim_create_autocmd("ColorScheme", { group = group, callback = function(args) if is_programmatic_change then is_programmatic_change = false return end local theme_name = args.match vim.uv.fs_open(theme_cache_path, "w", 438, function(err, fd) if err or not fd then return end vim.uv.fs_write(fd, theme_name, -1, function() vim.uv.fs_close(fd) end) end) end, }) load_theme()