Under Debian-Jessie, Emacs recognizes qualified special keys, e.g. C-<home>, in an X terminal (e.g. mate-terminal) but in TTY it does not.

I have corrected this by making a replacement keymap, loaded by adding KMAP=myReplacement to /etc/default/keyboard, but I had to invent totally arbitrary strings for the qualified keys that I wanted (specifically C-<home> and C-<end>).

I had to edit .emacs to match these strings. The coordination cost of this solution is too high. How do I discover what Emacs expects for these key sequences so that I only have to modify my OS keymap and not both this and .emacs?

  • I see that no one has responded to my question but that it has a few views. I have answered the question myself and my answer seems more concise than the sources that informed it. Should I post this? Remove the tumbleweed? Do nothing? Commented Jan 18, 2018 at 1:38

1 Answer 1


Adding keys requires modifications in both Emacs and TTY domains. This required coordination allows arbitrary strings for key codes but it is better to use key codes that might be recognized by other programs and not just Emacs. Key code strings from xterm stand the best chance of being more widely recognized. In Debian and Ubuntu with Emacs 24.5, the file /usr/share/emacs/24.5/lisp/term/xterm.el affords a convenient reference. It contains many define-key statements associating each keysym with the key code string emitted by xterm. I will demonstrate how to add C-home and C-end recognition. The method can be extended to any keysym recognized by Emacs under xterm. In this procedure I refer to the directory /site, which I use for my system extensions. Any directory will do.

In xterm.el see (define-key map "\e[1;5H" [C-home]) and (define-key map "\e[1;5F" [C-end]). To Emacs "\e" (and its vector equivalent "\M-") means ESC. In the TTY key map "\e" means plain "e" but "\033" means ESC.

  • Before changing the key map, do dumpkeys > /site/default.keymap
  • showkey reveals that the keycodes for home and end are 102 and 107
  • Create file /site/xtty.keymap containing:

    include "default.keymap"
    string F100 = "\033[1;5H" # [C-home]
    control keycode 102 = F100 # 102 = Home aka Find
    string F101 = "\033[1;5F" # [C-end]
    control keycode 107 = F101 # 107 = End aka Select
  • Create file /site/xttyemacs.el containing:

    (define-key input-decode-map "\e[1;5H" [C-home]) ; aka C-Find
    (define-key input-decode-map "\e[1;5F" [C-end])  ; aka C-Select
  • To each user's .emacs add:

    (if (equal (getenv "TERM") "linux") (load "/site/xttyemacs.el"))
  • Test this by loadkeys xtty.keymap and then run emacs and ask for key help (C-h k) on C-home and C-end. Also, make sure that the TTY command line is working correctly. If something has gone terribly wrong and the keyboard doesn't work, just reboot (using power key if necessary).
  • To /etc/default/keyboard append the declaration KMAP=/site/xtty.keymap. Note that there are no spaces around = and the filespec is not quoted. Exit and log back in to verify. You shouldn't have done this before verifying with loadkeys but if the keyboard isn't working you can restore the default key map by removing xtty.keymap. You can't do this from another TTY because the same key map applies to all of them. For this reason, it is good to have an active X session, which doesn't use the TTY keymap, before changing the KMAP declaration or xtty.keymap.
  • Emacs also understands the "\033" octal style of escape for ESC.
    – npostavs
    Commented Jan 21, 2018 at 22:49

Your Answer

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

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