How to: automatic Dark Mode upon sunset including Safari, iTerm, Vim in macOS

November 25th, 2018

I must confess: I didn’t understand Dark Mode in the beginning nor did I see the reason behind it. When Mojave launched I remember being particularly impressed by Apple’s attention to detail, however I thought it was a feature I wouldn’t use. I was wrong. After some friends praised it recently I was intrigued. Gave it another spin for a while but I didn’t like a few things: i) macOS couldn’t switch between Light and Dark automatically like in Night Shift, ii) there was no easy way to switch between Light and Dark in my terminal environment (iTerm and Vim) which I exclusively use with Solarized, and iii) Safari web pages stayed in their default daylight whiteness. So, I reverted back to classic macOS.

Yet, a comment on Reddit was the spark to continue anew the challenge to fix the issues in my setup and automate the whole process. Thanks, u/SciGuy013!

Automatic switch between Light and Dark mode in iTerm and Vim

Here’s how I did it without messing with Apple Script, complicated setups, or dabbling too deep into obscure configuration files harming the “platform agnostic” factor of my dotfiles.

  1. In System Preferences > Displays > Night Shift enable Night Shift and set it to a sunset to sunrise schedule. (If you didn’t have Night Shift already enabled smh.)
  2. Download Shifty. Shifty is a free menu bar utility that gives you more control over Night Shift. After installing and running it, right click and open Preferences. There, simply check Scheduled Dark Mode. When enabled, Dark Mode will be automatically set based on the Night Shift schedule. Optionally, also set a keyboard shortcut for toggling Dark Mode. I used control + option + command + D. 1

    PS. I use brew cask to install non-App Store Mac apps: brew cask install shifty.

  3. Download Dark Reader for Safari. This extension creates dark themes for websites on the fly. It doesn’t blindly invert colors, it’s fast, and it’s absolutely worth its price. After enabling it in Safari settings, set it to Auto which means whenever it detects macOS running in Dark Mode it will convert websites to Dark Mode, too. 2

If you don’t use iTerm or Vim you’re ready. Enjoy switching between Light and Dark Mode automatically across every app and web content upon sunset and sunrise. If, on the other hand, you’re interested in using this setup for iTerm and Vim carry on, we’re almost there.

  1. First, duplicate your existing iTerm profile and change the color scheme. iTerm > Preferences > Profiles for duplicating and then Colors for switching to another scheme. Personally, I use Solarized Light and Dark. Name the new profile Dark.
  2. Put the following in your .bash_profile or .bashrc:
    if [[ "$(uname -s)" == "Darwin" ]]; then
        sith() {
            val=$(defaults read -g AppleInterfaceStyle 2>/dev/null)
            if [[ $val == "Dark" ]]; then
                i
            fi
        }
    
        i() {
            if [[ $ITERM_PROFILE == "Terminal" ]]; then
                echo -ne "\033]50;SetProfile=Dark\a"
                export ITERM_PROFILE="Dark"
            else
                echo -ne "\033]50;SetProfile=Terminal\a"
                export ITERM_PROFILE="Terminal"
            fi
        }
    
        sith
    fi

    Here’s what we’re doing: first we check if macOS is running in Dark Mode. If yes, we tell iTerm to switch to the Dark profile and update the $ITERM_PROFILE environment variable. (Otherwise, nothing happens. The i() function allows you to switch manually between profiles.)

  3. In your .vimrc put:
    let iterm_profile = $ITERM_PROFILE
    
    if iterm_profile == "Dark"
        set background=dark
    else
        set background=light        " Set solarized background color
    endif

    Here’s what’s happening: every time iTerm runs in Dark Mode (which we’ve set macOS to enable upon sunset,) Vim will set its background to dark (= Solarized Dark.) Otherwise, it’ll run with Light as before.

Et voila! We’ve completely automated switching from Light to Dark mode as well as utilizing this setup to change and maintain state in Safari, iTerm, and Vim.

As you can see, we’ve completely decoupled dynamic color switching from bash and Vim functionality, thus using and maintaining a leaner and more modular development environment.

To sum up, essentially we created an elaborate chain of reactions starting and based on Night Shift that goes something like this: if Night Shift on > Dark Mode on > bash switches iTerm profiles > Vim switches background colors. Very simple and elegant.

If you’ve got a better idea on how to achieve this setup or a similar one, let me know.


  1. I also dislike random menu bar utilities (always try to keep the bare minimum on: Dropbox, Magnet, Wi Fi, time, and date. Not even 1Password visible. Trust me, this is good.)
  2. You can configure per-site settings or just dim. You can read more Dark Reader in this Reddit thread. The developer is also very hands on and helpful. Overall, great app.