The stuff I do

Changing my Vim colorscheme depending on the time of the day

- 467 words -

← Posts

Automatic colorscheme changer 🔗

A few years ago, a question on vi.stackexchange caught my attention. @oarfish wanted a way to change their colorscheme depending on the time of the day.

In a world where Apple has a tool by default on most of its devices to change the colorscheme from dark to light during the day and where a lot of developpers use extensively f.lux or redshift it does make sense to be able to change your favorite text editor colorscheme automatically too.

To add this feature I made a very simple plugin which takes for variables as its configuration:

The source code is on Github.

This plugin leverages the timer function of Vim >8 and neovim (See :h timers) to regularly check the current system hour, compare it to the configurations of the plugin and call :colorscheme accordingly.

This was interesting to create this plugin because I didn't have an opportunity to play with Vim's timers before. However after a few days of use I realized that I actually like my colorscheme and I don't want to change it during the day. So I soon stopped using this plugin but this gave me another idea.

Event based colorscheme changer 🔗

I noticed that my main pain point related to my colorscheme was when I switch vim to the diff mode using :h :diffthis. I realized that my current colorscheme didn't do as well as the previous one in diff mode. So I came up with a pretty simple trick in my .vimrc:

" Color configuration
    try
        " Define the default colorscheme and the one used in diff mode
        let g:colorsDefault  = 'forest-night'
        let g:colorsDiff     = 'jellybeans'

        " Set up the default colorscheme when vimrc is sourced
        execute "colorscheme " . g:colorsDefault
    catch
        echo "Colorscheme not found"
    endtry
" Diff configurations
    " Easier diff commands
    command! DT execute "colorscheme " . g:colorsDiff | windo diffthis
    command! DO execute "colorscheme " . g:colorsDefault | windo diffoff

The idea is very simple: Declare two variables holding the names of the colorschemes I want to use in normal mode and in diff mode and define some wrapper commands around diffmode:

These lines having been in my .vimrc for a few time now and so far they fit my needs. Maybe in the future I'll also explain with more details how I handle the diff mode in Vim.

← Posts


Related posts

Posts in the same category: [vim]


Comments


Back to top