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:
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 code
in iTerm2 for this to work?
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. IfControl + i
produces the^i
(tab
) character then adding more modifiers to the shortcut will still produce the same^i
code, at least when usingiTerm2
. This may be occurring at the OSX level or perhaps at the Unix level where atermcap
table handles the translation of "terminal keys" to "output codes." It can be observed withiTerm2
by using"cat -v"
or"vis -o"
to see the output being generated and is also seen when usingKey Codes
(withoutiTerm2
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 asCommand-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 theControl
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. YetKey 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, ifiTerm2
could only see the standard codes produced from various key presses and not also see the modifier flags theniTerm2
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 uniqueControl + Alt + i
determined code(s) out for the use oftmux
but OSX takes them and tosses out a few details before delivery, making the code appear to be just^i
when it finally arrives fortmux
to handle. The shortcut binding intmux
expects to receive the code we thought we were sending but along the way OSX has removed the information about theAlt (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
iniTerm2
, I want to be able to pressCommand-Control-i
and havetmux
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 forF1
(0x1b 0x4f 0x50
). Good keys to look at are the less used characters generated on a Mac by holding downAlt
(Option
) andShift
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.
+
button to add a new shortcut.Command
andControl
keys while you press the "i
" key.Send Hex Code
" in theAction
drop-down. (Trying to useTab
to leave the Shortcut field will change the value for the shortcut.)0x02 0x1b 0x4f 0x50
" (without the quotes and assuming that you are using the defaulttmux
escape character,^b = 0x02
).OK
button to commit your shortcut. ClosePreferences
.Within
iTerm2
, invoke atmux
session and define the key binding for the codesiTerm2
will send.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 usingCommand+Control+i
to move through panes vertically. If successful, you'll probably want to record your key-binding with the start-up bindings invoked bytmux
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.