Sending Control+Alt+ to terminal in iTerm2

itermkeyboardshortcut

When I press Control + Command + i I would like iTerm2 to send the shortcut as C-M-i (i.e. ctrl+alt+i) to my terminal.

I have

How can I do that?

I have tried with the following with no luck:
enter image description here

Any thoughts on how to get this to work?

Update:

I read the following here:

To have iTerm2 send "C-M-Space" to the terminal:
1) Open: Preferences > Profiles > some session you wish to edit > Keys.
2) Add a new key binding by pressing the "+" button.
3) Select the "Keyboard Shortcut" field, and press ctrl + option + space.
4) Select "Send Hex Code" in the "Action" list.
5) Type "0x1b 0x00" to the field below the list.
6) Click OK and close the preferences window.

I wonder if I can follow the same approach for my problem. How do I find out which Hex Code I need to send for Control+Command+i?

Update 2

After reading this thread, I installed Key Codes and found the following info for Control + Command + i. What specifically am I supposed to enter as a hex codein iTerm2 for this to work?

                                enter image description here

Best Answer

I finally sorted out the various issues to come up with a solution. The problem seems to be that if the Control key is part of the keyboard shortcut then no other modifier keys that are also pressed will change the code that is sent by that keypress. If Control + i produces the ^i (tab) character then adding more modifiers to the shortcut will still produce the same ^i code, at least when using iTerm2. This may be occurring at the OSX level or perhaps at the Unix level where a termcap table handles the translation of "terminal keys" to "output codes." It can be observed with iTerm2 by using "cat -v" or "vis -o" to see the output being generated and is also seen when using Key Codes (without iTerm2 running) which makes OSX the more likely culprit. That conclusion is supported by noting that the general user is not able to redefine certain keyboard shortcuts (without difficulty) that are reserved for basic OSX functions (such as Command-Tab for program switching).

It is possible to programmatically differentiate any set of modifiers pressed with a character key but those modifier flags are accessible programmatically within OSX and by a totally unique set of codes for every different set of keys pressed. To witness this, use Key Codes and while holding the Control key and pressing a character key and then doing the same while pressing additional modifier keys at the same time, note that it is always the same character code that is produced. Yet Key Codes has access to the various keys being held and is able to display them to us in the "modifier" field that it reveals. And, if iTerm2 could only see the standard codes produced from various key presses and not also see the modifier flags then iTerm2 itself wouldn’t be able to define the range of shortcut keys that it is actually able to define. On the other hand, after identifying each set differently modified keys in a unique manner, iTerm2 appears to pass it’s generated codes to OSX where they are handled in the same way as if they’d been keyed on a keyboard, resulting in identical output for groups distinguishable keystrokes. So, for example, iTerm2 may pass the unique Control + Alt + i determined code(s) out for the use of tmux but OSX takes them and tosses out a few details before delivery, making the code appear to be just ^i when it finally arrives for tmux to handle. The shortcut binding in tmux expects to receive the code we thought we were sending but along the way OSX has removed the information about the Alt (Meta) key, the same as it would do if the keyboard shortcut had been typed on a keyboard.

The solution is to find a key combination that won’t be used for something else and that can be transmitted without being changed to another key combination that you do want to use. That’s where Key Codes is useful since it let's you test many key combinations and find what the key presses are translated to be "unique enough." Here’s the process I used to make it work for me:

Goal: Running tmux in iTerm2, I want to be able to press Command-Control-i and have tmux execute the "select-pane -U"command.

  • Choose an esoteric key press that you won’t be using. Consider a multi-key sequence with the tmux escape character followed by another unusual key press making it much less likely to be needed. I’m choosing to send the standard tmux escape character (0x02 = ^b) followed by the ansi code sequence for F1 (0x1b 0x4f 0x50). Good keys to look at are the less used characters generated on a Mac by holding down Alt (Option) and Shift with another character key.

  • Within iTerm2 open Preferences and define the Command+Control+i shortcut in either the global key shortcuts or as a keyboard shortcut for a specific profile.

    • Press the + button to add a new shortcut.
    • Click in the Keyboard Shortcut field and hold down the Command and Control keys while you press the "i" key.
    • Use the mouse to select "Send Hex Code" in the Action drop-down. (Trying to use Tab to leave the Shortcut field will change the value for the shortcut.)
    • Type the string "0x02 0x1b 0x4f 0x50" (without the quotes and assuming that you are using the default tmux escape character, ^b = 0x02).
    • Press the OK button to commit your shortcut. Close Preferences.
  • Within iTerm2, invoke a tmux session and define the key binding for the codes iTerm2 will send.

    • Invoke tmux as you normally would.
    • In the tmux window type the command: "tmux bind-key F1 select-pane -U" (without the quotes.)

Test it by splitting your tmux window into several appropriate panes and try using Command+Control+i to move through panes vertically. If successful, you'll probably want to record your key-binding with the start-up bindings invoked by tmux on startup.

This method worked fine for me and should work well unless you have somehow used all the possible keyboard combinations for other uses. Methods like this one combined with practice and experience guessing the inner workings of a system should can help work around situations like this one that may feel foreign and overwhelming. Some parts of the above explanation are reasonable guesses as to what is occurring or that may assign blame to the wrong process but I hope that it’s helpful as a guide to how problems like this can be approached with less than a complete understanding of the whole system.