Automatic darkmode for iTerm
iTerm2 supports dark and light mode, but it won’t change the colors automatically.
But we’re lucky! iTerm has great scripting support. See the Scripting Documentation . We can use that to automatically switch profiles based on the system appearance.
Here’s how:
- Launch iTerm, then in the menubar click
Scripts
→AutoLaunch
→Install Python runtime
- Create two profiles and name them
Light
andDark
. It probably makes sense to create one profile to your liking with a light theme, duplicate it and set the dark theme on the duplicate. - Copy the script below into
$HOME/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/auto_darkmode.py
:
#!/usr/bin/env python3.7
import asyncio
import time
import subprocess
import iterm2
def is_dark_mode():
cmd = 'defaults read -g AppleInterfaceStyle'
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
return bool(p.communicate()[0])
async def set_profile(connection):
app = await iterm2.async_get_app(connection)
profile = "Light"
if is_dark_mode():
profile = "Dark"
partialProfiles = await iterm2.PartialProfile.async_query(connection)
for partial in partialProfiles:
if partial.name == profile:
full = await partial.async_get_full_profile()
# Set profile in _all_ sessions
for window in app.terminal_windows:
for tab in window.tabs:
for session in tab.sessions:
await session.async_set_profile(full)
return
async def main(connection):
await set_profile(connection)
while True:
time.sleep(2)
await set_profile(connection)
iterm2.run_forever(main)
If you run into issues, try running the script via the menubar Scripts
→ AutoLaunch
→ auto_darkmode.py
Similar to Automatic darkmode for Vim , this will check the system appearance every two seconds and set the profile accordingly.
Edit this page on GitHub