The latest beta (3.5) includes separate color settings for light & dark mode. Toggling dark mode automatically switches colors.
Vist iTerm2 homepage or use brew install iterm2-beta
to download the beta. Thanks @stefanwascoding.
- Add
switch_automatic.py
to~/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch
with:
#!/usr/bin/env python3
import asyncio
import iterm2
async def main(connection):
async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.APP, "effectiveTheme", None) as mon:
while True:
# Block until theme changes
theme = await mon.async_get()
# Themes have space-delimited attributes, one of which will be light or dark.
parts = theme.split(" ")
if "dark" in parts:
preset = await iterm2.ColorPreset.async_get(connection, "Dark Background")
else:
preset = await iterm2.ColorPreset.async_get(connection, "Light Background")
# Update the list of all profiles and iterate over them.
profiles=await iterm2.PartialProfile.async_query(connection)
for partial in profiles:
# Fetch the full profile and then set the color preset in it.
profile = await partial.async_get_full_profile()
await profile.async_set_color_preset(preset)
iterm2.run_forever(main)
Change Dark Background
and Light Background
to color presets you like.
- Enabled
switch_automatic.py
from Scripts menu.
There seems to be a bug with this script, where it spawns multiple processes that don't go away after iTerm2 has ended.
My laptop fans were constantly running and I saw bash was consuming 99% of a CPU. Looking more carefully I saw a screenful of this script running. After manually killing them all, CPU returned to normal.
I notice when I start up iTerm2, it launches two processes using ~/.config/iterm2/AppSupport/Scripts/AutoLaunch/auto_dark_mode.py. (I think that's normal, because one is the child of the other?)
But when you completely exit the iTerm2 application, those processes remain running. Then, if you launch iTerm2 again, two more processes are spawned for 4 total, and so on.
These processes aren't exiting when iTerm2 exits. I'm using iTerm2 on Mojave; I'll test it on M1/Big Sur as well.
[Edited for formatting.]