This idea came quite at random: Today I was switching back and forth between various tasks in iTerm2 and the terminal window in VS Code, and after a while I noticed that the colors were off … normally nothing you would care about, but after a while it started to bug me.

So I did what any good procrastinating engineer would do, I quickly put together a little Python script to extract the ANSI colors of iTerm2’s “Default” profile and print them out as JSON settings for VS Code. Enjoy. 😉

#!/usr/bin/env python3

import json
import plistlib
from os.path import expanduser

color_name_map = {
    "Ansi 0 Color": "ansiBlack",
    "Ansi 1 Color": "ansiRed",
    "Ansi 2 Color": "ansiGreen",
    "Ansi 3 Color": "ansiYellow",
    "Ansi 4 Color": "ansiBlue",
    "Ansi 5 Color": "ansiMagenta",
    "Ansi 6 Color": "ansiCyan",
    "Ansi 7 Color": "ansiWhite",
    "Ansi 8 Color": "ansiBrightBlack",
    "Ansi 9 Color": "ansiBrightRed",
    "Ansi 10 Color": "ansiBrightGreen",
    "Ansi 11 Color": "ansiBrightYellow",
    "Ansi 12 Color": "ansiBrightBlue",
    "Ansi 13 Color": "ansiBrightMagenta",
    "Ansi 14 Color": "ansiBrightCyan",
    "Ansi 15 Color": "ansiBrightWhite",
    "Background Color": "background",
    "Foreground Color": "foreground",
    "Selection Color": "selectionBackground",
}


def iterm_color_components2hex(iterm_color):
    return "#{:02x}{:02x}{:02x}".format(
        int(iterm_color["Red Component"] * 255),
        int(iterm_color["Green Component"] * 255),
        int(iterm_color["Blue Component"] * 255),
    )


iterm_plist = f'{expanduser("~")}/Library/Preferences/com.googlecode.iterm2.plist'
with open(iterm_plist, "rb") as fp:
    plist = plistlib.load(fp)

    # Get the 'Default' profile from the plist config
    # No idea why the key is called 'New Bookmarks'. ¯\_(ツ)_/¯
    bookmarks = plist["New Bookmarks"]
    default_bookmark = [d for d in bookmarks if d["Name"] == "Default"][0]

    vscode_colors = {}
    for iterm_color_name, vscode_color_name in color_name_map.items():
        key = f"terminal.{vscode_color_name}"
        value = iterm_color_components2hex(default_bookmark[iterm_color_name])
        vscode_colors.update({key: value})

    print("Add this to your Visual Studio Code 'settings.json':")
    print(json.dumps({"workbench.colorCustomizations": vscode_colors}, indent=2))