Increase/Decrease Font Size in iTerm2

33

5

Issue: I use laptop on the go, and connect to external monitor when in the office. Given that the external monitor is very large, I need to increase fonts in the existing iTerm2 window (usually one) and all tabs.
Workaround: I scale the fonts up 2-4 times (by pressing Cmd-+) for every tab I have open in a window (usually just one).

Question: Is there an easy way I could automate increasing/decreasing font size for all tabs of the current window? Or If I were to create two separate profiles, could I easily apply some profile to all currently open tabs in a window?

van

Posted 2015-02-12T09:37:26.657

Reputation: 433

Answers

27

There's a really shitty and buggy way to automate this, but I'll post it anyway.

You can create a new profile in the iTerm2's preferences (the Profile pane). Let's call it "LargeFont". You can clone it from the default one by pressing ⌘=.

Set the font size you want it to display in the newly created profile's Text pane.

Now here's the trick. You can't change either the font size or the profile of the terminal sessions using AppleScript (at least I haven't found a way), but you can execute commands in every session using AppleScript, and there's a custom escape sequence in iTerm2 that supports changing profiles for the session it was echo'ed in.

So, you can execute that:

echo -e "\033]50;SetProfile=LargeFont\a"

in every opened session to change the terminal's profile to "LargeText".

Now we can use AppleScript to automate the execution for all opened sessions:

tell application "iTerm"
    repeat with theTerminal in terminals
        tell theTerminal
            repeat with theSession in sessions
                tell theSession
                    write text "echo -e '\\033]50;SetProfile=LargeText\\a'"
                end tell
            end repeat
        end tell
    end repeat
end tell

Please note that it just writes the text (literally) into the each session, so if you have some text editor opened in one of your tabs - it won't work in it, and will paste the echo command in your code/configuration file instead. If you have a ping command running in one of the tabs - it won't work, too.
You should make sure there is no interactive stuff running in any of your shells.

You'll also have these commands left in your shell's history. You could bypass it by adding a space before the command itself (like echo -e ...), this works at least in zsh.

Here's the zsh function:

function iterm_change_profile() {
    osascript -e "
        tell application \"iTerm\"
            repeat with theTerminal in terminals
                tell theTerminal
                    repeat with theSession in sessions
                        tell theSession
                            write text \" echo -e \\\"\\\\033]50;SetProfile=$1\\\\a\\\"\"
                        end tell
                    end repeat
                end tell
            end repeat
        end tell"
}

So you could use it like that:

iterm_change_profile LargeFont

There's also a drawback - when you change the profile from the one with the larger font to the smaller one, the iTerm's window resizes significantly.

But, again, it's a really shitty way.

Igor Hatarist

Posted 2015-02-12T09:37:26.657

Reputation: 1 864

Thank you, I will give it a try. A similar thing I would like in iTerm2 would be to change also the Color Preset (Solarized-Dark and Solarized-Light depending on the surrounding light). I assume I will create a separate profile for that as well. – van – 2015-02-18T12:23:04.600

1Thanks, @Igor. It worked nicely, although as you mentioned it is not the cleanest way. And you can uncheck the option to increase/decrease the window to resize when the font size changes in General Preferences -> "Adjust Window when changing font size". – van – 2015-02-20T15:25:44.667

@van Haha, I totally missed that option, thanks! – Igor Hatarist – 2015-02-20T15:28:28.517

just found out another thing: if i use the TmuxIntegration, changing font size works across all Tabs (tmux windows/panes). Color is still just per pane though.

– van – 2015-02-20T20:30:27.920

0

In the current iTerm2 (version 3.3.7), there's View > Size Changes Update Profile which solves the issue:

To change the font size regardless of profile, see this iTerm2 issue where a script is provided.

Borek Bernard

Posted 2015-02-12T09:37:26.657

Reputation: 11 400