# zigford.org

About | Links | Scripts
Sharing linux/windows scripts and tips

### Dynamic colorscheme in Vim

June 15, 2019 — Jesse Harris

Most folk know about `:colorscheme` in Vim. In this post I will show how I setup my vimrc to change the colorscheme based on the time of day.

This method is quite simple. It checks the time when my vimrc is loaded and depending on the result set's the colorscheme accordingly. There may be smarter ways to achieve this, by setting a timer to check periodically. However I find this approach good enough for me.

This approach will work on Linux, macOS and Windows. In the example below, I use the colorscheme gruvbox and simply switch between light and dark background modes.

            " Format the *nix output same as windows for simplicity
            let s:time = has('win32') ? system('time /t') : system('date "+%I:%M %p"')

            let s:hour = split(s:time, ':')[0]
            let s:PM = split(s:time)[1]

            if (s:PM ==? 'PM' &&
               (s:hour > 7 && s:hour != 12)) ||
               (s:PM ==? 'AM' &&
               (s:hour < 8 || s:hour == 12))
               set background=dark
            else
               set background=light
            endif
            colorscheme gruvbox

In this example, I have dark mode enabled from 8pm until 8am. Outside these hours, light background is set.

As you can see, in order to only have one method of parsing the systems date/time, I simply use the `*nix` flexible date formatting to make it appear similar to Windows, and then I only need 1 parsing function.

Tags: vim-tips, vim