1

One of my Emacs keybindings is C-', which works well in GUI. In terminal however, it is not being recognised. I understand that I need to figure out the actual characters sent to the terminal by C-' and map it in the emacs config.

Following the advice of Where do I find a list of terminal key codes to remap shortcuts in bash?, sed -n l is returning back to me a an empty line, even without the ending $. Does Terminal not recognise the C-' sequence at all?

1

Terminals transmit bytes, not keys. Keychords like Ctrl+' have to be encoded as sequences of bytes. Apart from printable characters with no modifier or with just Shift, most keychords have no corresponding characters, and are instead transmitted as escape sequences, beginning with the escape character (the character with the byte value 27, which you can write as \e in Emacs strings). But many keychords don't have a traditional standard escape sequence, and many terminals either don't transmit these keychords or strip information about modifiers (transmitting Ctrl+' as just the ' character).

Some terminals allow you to configure escape sequences for each keychord. In Terminal.app, you can do this from the keyboard preferences.

For Ctrl+', pick either \033[39;5~ or \033[27;5;39~: these are two emerging standards, the libtermkey scheme and the xterm scheme. See Problems with keybindings when using terminal for more information.

Emacs translates escape sequences into its internal key representation through input-decode-map or local-function-key-map (or function-key-map before Emacs 23). Put either of these in your init file:

(define-key input-decode-map "\033[39;5~" [(control ?\')])
(define-key input-decode-map "\033[27;5;39~" [(control ?\')])
  • Thanks a lot. This explains it. I wonder though you're familiar with OS X terminal. Following the instructions in the link, it seems there are only a set of keys I can customise, and ' is not one of them. As far as you know, can it be done in some .rc file? – darksky Sep 14 '15 at 14:46
  • @darksky I'm not familiar with OSX, sorry, I'm only going by the documentation I've found online. If the interface described in the link doesn't let you customize all key presses, it might be possible in some configuration file, or you might need to use a different terminal emulator. – Gilles Sep 14 '15 at 15:53
0

I tried using showkey and noticed that c-' is identical to ' alone, and after reading the referred answer and trying it too, I tested almost all of the keys on my keyboard with showkey and found out that most of them have a ^key output when pressed with control, like ^C for pressing c-c, but only few keys are not affected by pressing c-, for example not only ' but , too is not affected, and the dot . when pressed with c- didn't print not even a dot, it just did nothing, so I think your binding isn't working in terminal because it's like pressing plain ' which does nothing! But I can't explain why it works under X maybe because it can recognize it.

I encourage you to try out your keys with showkey --ascii and hope this answer was helpful.

  • This is completely irrelevant. showkey is a Linux console utility, which has no relevance to OSX. The reason X sees key combinations that programs running on a text mode console don't is that X accesses the keyboard driver directly, without going through the console terminal driver. – Gilles Sep 12 '15 at 0:18
  • Excuse me! My bad, I didn't really notice the word "OS X" in the title and only focused on the question witch never mentioned "OS X" again. I'm running Debian 8 and don't have access to OS X, but I'll leave the answer because it has info about the terminal not recognizing the c- pressed with ', which may be beneficial. – Bahax Sep 12 '15 at 0:23
  • And excuse me again, but BTW I only used showkey on my machine to find out what you confirmed in your elaborate answer that c-' is handled as plain ' so I don't think that was "completely irrelevant"! Although I do respect your 289k rep, but I think you ware a little hasty. – Bahax Sep 12 '15 at 0:30
  • This is irrelevant as well, since different terminals transmit keychords differently. Note that the asker stated that C-' transmitted nothing (not even '). – Gilles Sep 12 '15 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.