7

How can I send Ctrl+; in iterm2 (emacs)?

There seems to be a solution here, here and here

These are close to what I want, but I am still unable to make it work.

2
  • 1
    Adapting my answer to the third link and sending 0x1B 0x3B doesn't work?
    – Daniel Beck
    Mar 20, 2014 at 18:54
  • Thanks for the response. I tried it, and realized 0x1B = ESC not control? (emacs sees it as M-; not C-;)
    – eugene
    Mar 21, 2014 at 1:14

3 Answers 3

1

The way I make these key combinations work is to go to the Keys section of the iTerm prefs and create a shortcut for ^; that sends some escape sequence, like ^[[aa (you can replace aa with anything, but be aware that some things are mapped to actual keys). Then in your .emacs, create a keyboard shortcut for what you want it to map to, like

(global-set-key (kbd "C-[ [ a a") 'the-function-you-want-to-map-to)
9
  • What exactly do you have to do in iTerm here? When binding to `^;^ I have an action field I can set. Should I set it to "Send Escape Sequence"? And what should the next field then be?
    – rien333
    Feb 26, 2018 at 23:37
  • Yes, use "Send Escape Sequence", then add the code without the ^[ (in my example, [aa). The ^[ (Control-[) control code is "escape".
    – asmeurer
    Feb 27, 2018 at 1:55
  • Thank you, this works! When inputing an escape sequence however, iTerm shows ESC + [ _your escape sequence_ ], indicating that it will actually send an escape key instead of ctrl key. (in my case, ESC seems to map to alt/M-) Just something weird.
    – rien333
    Feb 27, 2018 at 13:17
  • Control-[ (control-left bracket) and ESC are the same thing, at least as far as a terminal is concerned (ESC is also called M- in emacs, and is often mapped to alt/option). You can test this in emacs. ESC-f and C-[ f both do the same thing (and in fact, if you type C-[, it should show ESC at the bottom).
    – asmeurer
    Feb 27, 2018 at 20:15
  • Nope, not on my iTerm with emacs -nw. For me, ESC is definitely the same as pressing alt/option (which I've never consciously changed), although it does show up as for example ESC-f instead of M-f.
    – rien333
    Feb 28, 2018 at 9:54
11

How to bind a key sequence to the control key in Iterm2

  1. Download the free application “Key Codes" from the app store. Install it from the link below and then open it.

  2. Hit the Ctrl+some key in this case I’ll use Ctrl+w.

    How to get the hex code

  3. The code in the red square is the code we want to send to the terminal

  4. Now map your key sequence using in iterm, preferences->Keys->+.

How to map the command to the hex code

  1. This will now map opt+←delete to Ctrl+w in the terminal, but you can use it to map any key to another one. I know Ctrl+w is a bad example for Emacs since it doesn't support Ctrl+w but you can use this methodology for Emacs compatible shortcuts.
1
  • I don't think the unicode syntax you pasted is supported.
    – rien333
    Feb 26, 2018 at 23:46
1

The simplest way would be Preferences -> Profiles -> Keys --> toggle on the Report modifiers using CSI u.

Here is the documentation of this option and more about CSI u.

There are some detailed discussions in this thread.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 2, 2021 at 21:17

You must log in to answer this question.

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