Change iTerm2 Default profile preset with "defaults write..."

I want to do this in my dotfiles script.

I managed to download the preset I want using curl and open it, but I don't know how to set it in the default profile:

curl -L https://raw.githubusercontent.com/chriskempson/base16-iterm2/master/base16-ocean.dark.itermcolors > /tmp/base16-ocean.dark.itermcolors
open /tmp/base16-ocean.dark.itermcolors

I tried AppleScript, but I never used it, and it seemed to be too much work for little... I believe it could be a simpler way, using defaults write, for example.

Any ideas?


Solution 1:

To expand on the existing answer, if you load the colours you'd like once then you can export your preferences list file from the 'General' tab to a location in your dotfiles.

Thus, in my setup script, I have:

defaults write com.googlecode.iterm2 "PrefsCustomFolder" -string "/Users/ojf/dotfiles/iterm"
defaults write com.googlecode.iterm2 "LoadPrefsFromCustomFolder" -bool true

As long as your custom folder plist also says to load from the same custom folder (!) you'll be fine.

Solution 2:

Sorry but due to how the iTerm2 preferences are stored, it isn't possible to do what you want.

Color themes are not files that iTerm2 points to like you are thinking. Once you download a theme, you open it and iTerm2 imports the XML and adds it to the preference file. You then edit the preferences for the profile to use the new theme.

Each profile contains a copy of the theme data. It isn't just pointing to the loaded theme XML info. This way if you edit/delete the theme, the profile continues to use the same theme colors.

All the profiles are stored in a single dict and one of them is set as the default. There isn't just a 'default' profile that you could edit or update with that theme.

If you really want to see the huge mess that is the iTerm2 preferences, run

defaults read -app iterm

What you have done so far is probably the best you are going to get. Just keep a copy of the itermcolors theme file that you want in your dotfiles then open it to add to the iTerm2 theme list when you are setting up a new user.

Another option is to configure iTerm2 like you want, complete with profile themes, then back up the ~/Library/Preferences/com.googlecode.iterm2.plist file into your dotfiles. That one plist will contain everything you need to set up iTerm2 like you want.