1

I've looked on google, stack exchange, GNU emacs manual, xterm manual, etc. and still haven't found an answer that satisfies my problem(s).

Problem:
Using Systems: Ubuntu 16.04, xterm, emacs-nox
I want to bind the keys Ctrl+i Ctrl+m Ctrl+[ and Meta+Shift+o to any command I want.
But the problem seems to be that all of said key sequences are bound to:
Tab, Return, Escape, and Control Code ESC 0 respectively becuase that's just the way terminals work.

So how do I keep the functionality of the Tab, Return, and Escape whilst having said keybinds at my disposal?
Do I have to remap the keys in the terminal somehow? If so, how?
I will do anything to make this work.

1 Answer 1

2

If you want to be fiddly then you can (mostly) do it. For instance, for C-i and C-m, add to your .Xresources:

XTerm*VT100.Translations: #override \
     Ctrl       <KeyPress> I: string("^[OJ") \n\
     Ctrl       <KeyPress> M: string("^[OK")

where ^[ needs to be replaced by the escape character, entered (in emacs) with C-q ESC (C-q runs quoted-insert which inserts the next character literally). The actual escape character can't be displayed in the browser, so I'm using the string ^[ to represent it, as that's how it's typically displayed. \n\ is needed at the end of each line that is continued.

^[OJ and ^[OK were chosen as these sequences seemed to be free. (You can determine which sequences are "free" by inspecting input-decode-map within Emacs, via C-h v input-decode-map RET. The map is effectively a "tree". The numerical values are the codes of the corresponding characters — for instance O is 79. You can determine the code of the letter O by evaluating ?O etc.)

Load the .Xresources with xrdb -load .Xresources and restart your xterms.

In your init.el add:

(define-key key-translation-map (kbd "^[OJ") (whatever-you-want-1))
(define-key key-translation-map (kbd "^[OK") (whatever-you-want-2))

where again ^[ needs to be replaced by the escape character. Replace (whatever-you-want-[12]) appropriately.

For some more details see:

https://kb.iu.edu/d/abko

https://superuser.com/questions/453993/xterm-key-translations

https://manpages.debian.org/stretch/xterm/xterm.1.en.html#Default_Key_Bindings

26
  • It's not working for me :/ A couple things are happening: 1) When I did xrdb -load .Xresources and restarted my xterm it like inverted my colors making the background white and text black (I haven't changed the default colors of the terminal); and made strings (in between " ") invisible within emacs. 2) When I did (define-key key-translation-map (kbd "^[OJ") 'previous-line) all emacs does is spit out the string "^[OJ in the buffer. Apr 7, 2018 at 5:22
  • Update: The first issue seems to be unrelated; when I log out and log back in the terminal colors are back to normal. Apr 7, 2018 at 5:38
  • 1
    Update: When I describe the key in emacs using Ctrl-h k for Ctrl-i the description is: ^ runs the command self-insert-command, which is an interactive built-in function in 'C source code'. It is bound to many ordinary text characters. Not sure if this is a clue or not. Apr 7, 2018 at 6:51
  • 1
    Yes, any number other than those taken could be used. Regarding the "<>" syntax, it seems after testing (but I can't easily find anything in the documentation), that you can use an arbitrary string (even "<abcdefgh>") — so you could just use the non-standard, but descriptive "<control_i>" etc. It appears to work and I don't think it would break anything. (One issue that might come up in the future is if you start using a terminal emulator other than xterm — since they have different translation maps — but it's independent of the choice of the "<>" key names.)
    – aplaice
    Apr 8, 2018 at 8:30
  • 1
    Regarding interpreting the translated bindings in bash — I don't think there's any simple solution. You could try to use the bind bash command (e.g. bind '"\eOJ": ""' where \e is the escape character (yes, different conventions in different programs, for the same character, are annoying...) and "" is deliberately the empty string — see the info manual for details), but things are likely to start getting very brittle. As for moving the conversation to chat, I'm not certain how to do that.
    – aplaice
    Apr 11, 2018 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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