This file: http://user.it.uu.se/~embe8573/tty-emacs-keys.txt https://dataswamp.org/~incal/tty-emacs-keys.txt The Linux console: special shortcuts in Emacs --------------------------------------------- This tutorial describes how to get special shortcuts (e.g., C-0) in an Emacs instance that runs in a tty. This Linux console/VT/tty solution works on Debian, and probably on many other distros as well, with only minor or none modifications required. (VT = Virtual Terminal, tty = teletypewriter, console = console - all the same at least with respect to this tutorial.) If you are inpatient, you can deduct how it is done from these two configuration files - * console remap: http://user.it.uu.se/~embe8573/conf/remap.inc * Emacs bindings: http://user.it.uu.se/~embe8573/conf/emacs-init/console-keys.el - otherwise the below tutorial describes how the get the C-0 shortcut working. In the file /etc/console-setup/remap.inc type control keycode 11 = U+0110 Obviously, 'control' is the "Ctrl" key, or C- in Emacs notation. (There is also "alt" and "shift", and they can be combined.) '11' is the keycode for the key '0' in the console, which you learn from invoking showkey(1) and hitting that selfsame key. -*- Note: If you use tmux on top of the tty, you might get this error with showkey: Couldn't get a file descriptor referring to the console If so, do either $ sudo showkey # every time or if that doesn't work/is inconvenient $ sudo chmod u+s /usr/bin/showkey # once If showkey is somewhere else than /usr/bin/showkey, do $ type showkey in bash (or zsh) to find out where it is. -*- Let's return to the line control keycode 11 = U+0110 This syntax reads: assign the shortcut C-0 so that when pressed, instead of the normal behavior it inserts the char which has Unicode designation U+0110. That char turns out a diamond in a tty, so it must be something fancy that we'll never need for anything else. The Unicode (the particular code, i.e. U+0110) is arbitrary in the sense that it doesn't matter what it is as long as the char isn't in use already. Make it exotic! When you have found one such exotic char, it is easy to find several others just by adding one for each shortcut you add. Just add one! Classic comp-sci stuff. Yes, the notation is hexadecimal. -*- Note: When I posted this tutorial on gmane.emacs.help, Yuri Kahn wrote that: > If you are going to recommend people to > hijack arbitrary Unicode characters for > extended keycodes, at least tell them to > pick codes from the "Private Use Area" > (U+E000 up to and including U+F8FF). > It is the Unicode equivalent of RFC 1918 > IP addresses — guaranteed to never have > an official meaning. -*- Now save the remap.inc file and use this zsh function (or a bash equivalent, or just type the command) to bring up the modified keyboard and see that it works: #! /bin/zsh lkeys () { loadkeys -q -c -s /etc/console-setup/remap.inc } Again, do the '+s' thing if you don't want to do sudo each time with loadkeys(1). After invoking 'lkeys', hit C-0 in a tty. Now, the diamond should appear! -*- Note: The options above to loadkeys are not relevant to this tutorial, but I use the function for other purposes as well, and it is bad luck to modify a function in a text document. Briefly, those options are: -c --clearcompose -q --quiet -s --clearstrings -*- Next, in an Emacs init file - ~/.emacs, or if you want to put such things somewhere else, and then do `load-file' from the main init file - anyway, in such a file, type: (define-key input-decode-map [?\u0110] [C-0]) The same Unicode position is used, only the notation differs slightly. The "C-0" is an arbitrary name, but it is a good name that is descriptive and adheres to the Emacs notation for keys. Evaluate the `define-key' function. Done! To try it, hit the key C-0, and it should say: (translated from diamond) is undefined This means you can bind it to whatever, using the ordinary Emacs syntax: (global-set-key [C-0] #'switch-to-buffer) One last hint: Because you want to load the modified keymap each time you start the system, but without having to input the super user password, one way to do that (at least on Debian) is to put: loadkeys /etc/console-setup/remap.inc > /dev/null in /etc/rc.local On my Raspberry Pi Raspbian, another Debian fork, I have it in ~/.zprofile instead. Tutorial by: Emanuel Berg (moasenwood AT zoho DOT eu) Last modified: July 21, 2015 June 25, 2017