1

I typically have multiple terminal sessions across different screens. Since opening the terminal is such a frequent task, I have a hotkey bound to open -b com.googlecode.iterm2 . with Karabiner-Elements. This way I can open a terminal from anywhere with one keypress.

Unfortunately if a terminal window is already open, this hotkey does not create a new window, but creates a new tab in existing windows. The existing window is often some long running "background" process like top or watch, meant to run undisturbed on a secondary screen while I do my work on the main screen. When a new tab is created in it, this causes problems for my workflow:

  • Hides the monitoring session, which I probably want to see
  • Opens the new terminal on the secondary screen, even if I had been working on the primary one
  • Moving the terminal to my main screen also moves the monitoring session to the main screen, so I have to move it back when done
  • Separating the tab requires finicky and tedious mouse dragging

These may seem like minor complaints, but I develop software and have to go in and out of terminals a lot. Depending on what I'm working on, I may need to open and close new terminals every few minutes, sometimes multiple times a minute. Reusing the same terminal is not really an option because often the terminals have distinct sessions (eg. TUI programs that run continuously, SSH to multiple different machines).

Is there a command I can bind that also creates a new iTerm2 window?

In theory I could command-tab to the terminal first and use a hotkey to make a new window. This has 2 problems:

  • Instead of one key press, I need to make several key presses to switch windows first.
  • Doesn't handle edge case where terminal is not running already.
2
  • Spaces is not designed to handle a single app across more than one Space/Desktop. That's going to be your major hurdle, though idk specifically about iTerm.
    – Tetsujin
    Commented Mar 25, 2020 at 18:31
  • @Tetsujin If I go to the Dock, right click the iTerm2 icon, and select "New Window", it creates a new window on the correct screen as I would expect. The problem is that this requires me to use the mouse, so I was hoping there's a command that does it.
    – Donentolon
    Commented Mar 25, 2020 at 19:28

3 Answers 3

3

This is already built into iTerm2. See the Hotkeys section in their documentation.

  • Show/hide all windows with a dedicated, system-wide hotkey
  • Create a dedicated hotkey window.

    In its simplest form, it's a system-wide terminal window that you can open with a hotkey.

You can find these options in Preferences -> Keys

OSXDaily has an excellent write up on this (with pictures)

1
  • This is not what the OP is asking for. This hotkey, while global, is assigned to a single terminal. If you press it repeatedly it will open and close a single terminal, not open new terminal windows.
    – PKKid
    Commented Nov 23, 2021 at 1:46
0

One way is to tell iTerm2 what to do through AppleScript. You can bind keys in Karabiner Elements to call the shell command osascript to accomplish this.

Example iterm2.json for this, using command-3 to open a new iTerm2 window using the default profile:

{
"title": "New iTerm2 Window",
"author": "Ahnberg <[email protected]>",
"rules": [ {
        "description": "New iTerm2 Window",
        "manipulators": [ {
                "type": "basic",
                "from": { "key_code": "3",
                          "modifiers": { "mandatory": [ "command" ] } },
                "to": [ { "shell_command": "osascript -e 'tell application \"iTerm\" to create window with default profile'" } ]
            } ]
    } ]
}

Put the json file in ~/.config/karabiner/assets/complex_modifications/ and then add it by going into Karabiner Elements preferences, selecting the "Complex Modifications" tab and clicking "+ Add Rule" in the bottom, and finally selecting "+ Enable" next to the rule.

Note: AppleScript support for iTerm2 is currently in maintenance mode and might be removed in the future. They currently recommend using the Python API.

0

Though it seems not to be a proper solution, you can try to use the drop-down terminal provided by iTerm in the Preferences->Keys->Hotkeys setting to create a hotkey window.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .