Vim Color Scheme and Gnome Light/Dark Mode


Now that Gnome has a feature to toggle between dark and light color schemes on the fly from the quick settings menu, it would be nice to make vim respect these color scheme changes. I've tested it on Fedora 39 with Gnome 45.

Vim Color Scheme

I'm using Lucius theme for vim. The trick boils down to doing a system call to gsettings get org.gnome.desktop.interface color-scheme and seeing if it's set to default or prefer-light. Anything else (usually prefer-dark) means dark mode. Add the following to your .vimrc:

let $gnome_color_scheme = system('gsettings get org.gnome.desktop.interface color-scheme 
        \ | tr -d "''"
        \ | tr -d \\n')

colorscheme lucius
if $gnome_color_scheme == "default" || "prefer-light"
    LuciusWhite
else
    LuciusDark
endif

gsettings returns it's output surrounded by single quites and adds a newline, so we have to use tr to remove all that junk. Vimscript has to escape single quote by putting another single quote in front of it, that's why tr -d "''" is the way it is. Same with backslash in front of \n.

Lightline Color Scheme

I'm also using lightline status line. Lightline's solarized, PaperColor, and one colorschemes automatically adjust based on background color (light or dark) when vim is opened, although the plugin doesn't do this when vim is already opened. To get lightline to work we have to do the following.

set laststatus=2
set noshowmode
let g:lightline = { 'colorscheme': 'one', }
autocmd OptionSet background
        \ execute 'source' globpath(&rtp, 'autoload/lightline/colorscheme/one.vim')
        \ | call lightline#init()
        \ | call lightline#colorscheme() 
        \ | call lightline#update()

The only downside to all this is that if you already have vim open you have to do :so ~/.vimrc to change the color scheme. You could bind that to a key, if you don't feel like typing the command out every time.

You can view my full vimrc here.