57

I am using the Solarized color scheme in iTerm2 and it comes with two themes: one with a light background for daytime hours and one with a dark background for nighttime hours.

I am currently switching between them by going to the iTerm2 preferences, clicking Profiles, then Colors, and finally selecting the theme from the Load Presets menu.

My question is: is there a way to bind this to a keyboard shortcut?

Richard Jones
  • 5,928
  • 3
  • 20
  • 14

8 Answers8

51

This works on iTerm2 (version, 3.0.8 through 3.0.13)

  1. In iTerm's preferences, select the keys tab.
  2. There's a table called key mappings
    • click on the + at the bottom of the table
  3. Choose a keyboard shortcut
  4. From the dropdown menu, choose the load color preset option
  5. You'll get another dropdown menu to choose the color preset you want

Keys Tab]([https://i.sstatic.net/oJ8p1.png)

It works across all panes and windows and its setting is persistent across sessions.

Burgi
  • 6,768
David D
  • 611
23

I found another solution from this site:

Declare a function in your .bashrc or .zshrc: it2prof() { echo -e "\033]50;SetProfile=$1\a" }

Then you can switch profile from CLI with it2prof ProfileName

ejel
  • 446
14

Note: This is not a complete solution, since it isn't entirely keyboard based, but it's the fastest method I have found to switch between color schemes. Hopefully others find it useful.

  1. Create a profile for each color scheme you want to use (iTerm -> Preferences -> Profiles)
  2. Switch between profiles in your current session (Command-I or View -> Edit Current Session)

One keyboard chord and one mouse click.

Richard Jones
  • 5,928
  • 3
  • 20
  • 14
2

Here's what I'm doing to solve this issue. This won't be a good solution if you don't use tmux or screen because it involves quitting and relaunching iTerm. In Preferences -> General there is a checkbox for "Load preferences from a custom folder or URL." Check that box and save your settings (make a copy of ~/.iterm). Then change to the other color scheme and save again. Then I made a script that toggles between the two. If you're using tmux, iTerm quitting and reopening is totally inconsequential so it works for me.

pkill iTerm
sleep 0.2
rm -r ~/.iterm

if [[ -z $SOLARIZED_TOGGLE ]]; then
    export SOLARIZED_TOGGLE=1
fi

if [ $SOLARIZED_TOGGLE = "1" ]; then
    cp -rf ~/.iterm_dark ~/.iterm
    export SOLARIZED_TOGGLE=0
else
    cp -rf ~/.iterm_light ~/.iterm
    export SOLARIZED_TOGGLE=1
fi

env -i open -a iTerm
wilywampa
  • 153
1

According to this gist by the user FradSer,

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.

I installed the iTerm2 3.5.0beta11 (OS 10.14+) from https://iterm2.com/downloads.html and now I can specify different colors for light and dark modes:

profiles-colors

Toggling light and dark mode in OSX now automatically toggles these colors for me.

sp3ctum
  • 503
  • 1
  • 5
  • 7
0

In the current version Build 3.1.4:

  1. Open Preferences.
  2. Under "Profiles" create various Profiles with the desired colors, fonts, etc.
  3. Switch to "Preferences" -> "Keys".
  4. Under "Key Mappings" add "+".
  5. Under "Action:" select "Change Profile".
  6. Set the desired "Profile" and record the desired "Keyboard Shortcut".

enter image description here

Jaime Cham
  • 251
  • 2
  • 3
0

As of at least version 3.3.9

  1. Create multiple profiles
  2. In Preferences -> Profiles -> General, you should see a "Shortcut key" dropdown menu that allows you to select different shortcuts for different profiles

enter image description here

0

I found an apple script that toggle between solarized themes in this repository. I modified the script Toggle Solarized.applescript a bit to make it work on iTerm 2.9 and put it in an executable script for easy theme switching.

It's not perfect though because it doesn't modify your profile (after looking in the iTerm2 dictionary, you apparently can't do that with apple script). So the next time you open a tab, it won't keep the same color. But it does help.