term-keys - lossless keyboard input for Emacs
This package allows configuring Emacs and a supported terminal emulator to handle keyboard input involving any combination of keys and modifiers.
Table of Contents
Introduction
Generally, terminal emulators and applications running in a terminal cannot reliably transmit and receive certain keystrokes (keys in combination with modifiers).
For some key combinations, there is no consensus on how these events should be encoded on the wire (F-keys and movement keys plus modifiers);
some other key combinations cannot be represented at all (such as Ctrl1 or CtrlShiftA).
This can be an impediment for Emacs users, especially when you've already configured your Emacs in an environment unrestricted by these limitations (X11) and now wish to use it in the terminal as well.
The term-keys package allows solving this problem by giving these key combinations a custom, unique encoding, overcoming the limitations of the protocol implemented by the terminal emulators.
Installation
To install term-keys, first add my ELPA package archive. Add this to your init.el and evaluate it:
(require 'package) ; you may already have this line
(add-to-list 'package-archives
'("cselpa" . "https://elpa.thecybershadow.net/packages/"))
Then, install it like any ELPA package (M-xpackage-installRETterm-keys).
Setup
Setting up term-keys is a three-part process:
- Configure Emacs;
- Configure
term-keys (optional);
- Configure your terminal emulator.
See the sections below for more information.
Configuring Emacs
Setting up term-keys is as easy as:
(require 'term-keys)
(term-keys-mode t)
This will automatically set up current and new TTY frames to decode term-keys key sequences.
If you prefer to enable it for each frame manually, you can do so by invoking (term-keys/init).
Configuring term-keys
term-keys is very configurable. Most things can be changed via Emacs' customize interface - use M-xcustomize-groupRETterm-keys to access it.
Some of the supplementary code for terminal emulator configuration can be configured as well.
Because it's not loaded by default (as it's only necessary for initial configuration), you need to load it explicitly before configuring it, e.g. using M-:(require 'term-keys-konsole), and only then invoking Emacs' customize interface.
See the terminal's section in this file for more information.
The only part of term-keys which cannot be configured via the customize interface is the set of key combinations to support, as (counting all possible keys plus combinations of modifier keys) their total number is very large.
For this purpose, term-keys allows specifying the name of a function to invoke, which shall implement this logic.
See the documentation of term-keys/want-key-p-func and term-keys/want-key-p-def (as well as the definition of the latter) for more information.
Terminal Emulators
Each terminal emulator has its own method of configuration. Consult the section corresponding to your terminal emulator of choice below.
Note that you will need to update the terminal emulator configuration every time you change the term-keys configuration.
urxvt (rxvt-unicode)
There's three ways to configure urxvt with term-keys: via command-line parameters, via X resources, or by invoking a term-keys function from Emacs.
Command-line parameters
Command-line configuration consists in adding the key bindings to the urxvt invocation directly.
You can use term-keys/urxvt-script to create a shell script in this manner:
(require 'term-keys-urxvt)
(with-temp-buffer
(insert (term-keys/urxvt-script))
(write-region (point-min) (point-max) "~/launch-urxvt-with-term-keys.sh"))
Afterwards, you can run e.g.:
$ sh ~/launch-urxvt-with-term-keys.sh -e emacs -nw
This will launch Emacs under an urxvt instance configured for term-keys.
X resources
X resource configuration consists in adding the term-keys configuration to the X resources.
The X resources are global (per X session), and will apply to all newly-started urxvt instances.
You can use term-keys/urxvt-xresources to create the necessary configuration in this manner:
(require 'term-keys-urxvt)
(with-temp-buffer
(insert (term-keys/urxvt-xresources))
(append-to-file (point-min) (point-max) "~/.Xresources"))
Then use xrdb to load the file into memory:
$ xrdb -merge ~/.Xresources
Direct invocation
In addition to generating a static configuration as a shell script or X resources, you can ask term-keys to invoke urxvt directly.
This has the benefit that it will always use the up-to-date term-keys configuration, but the downside that it must be done by invoking emacs or emacsclient.
For example:
$ emacsclient --eval '(term-keys/urxvt-run-emacs)'
See the term-keys/urxvt-* definitions for more urxvt-specific help code.
You may also want to disable some default urxvt shortcuts which may interfere with using Emacs.
Consider adding something like this to your ~/.Xresources:
URxvt*iso14755: 0
URxvt.keysym.Shift-Insert: builtin-string:
URxvt.keysym.M-S: builtin-string:
xterm
xterm is configured nearly identically as urxvt;
thus, this section will be very similar to the urxvt section above.
Note: xterm supports an extended input mode; see modifyOtherKeys et al in the xterm(1) man page.
You may find it sufficient for your use case even without term-keys.
As with urxvt, there's two ways to configure xterm: via command-line parameters or X resources.
Command-line configuration consists in adding the key bindings to the xterm invocation directly.
You can use term-keys/xterm-script to create a shell script in this manner:
(require 'term-keys-xterm)
(with-temp-buffer
(insert (term-keys/xterm-script))
(write-region (point-min) (point-max) "~/launch-xterm-with-term-keys.sh"))
Afterwards, you can run e.g.:
$ sh ~/launch-xterm-with-term-keys.sh -e emacs -nw
This will launch Emacs under an xterm instance configured for term-keys.
X resource configuration consists in adding the term-keys configuration to the X resources.
The X resources are global (per X session), and will apply to all newly-started xterm instances.
You can use term-keys/xterm-xresources to create the necessary configuration in this manner:
(require 'term-keys-xterm)
(with-temp-buffer
(insert (term-keys/xterm-xresources))
(append-to-file (point-min) (point-max) "~/.Xresources"))
Then use xrdb to load the file into memory:
$ xrdb -merge ~/.Xresources
In addition to generating a static configuration as a shell script or X resources, you can ask term-keys to invoke xterm directly.
This has the benefit that it will always use the up-to-date term-keys configuration, but the downside that it must be done by invoking emacs or emacsclient.
For example:
$ emacsclient --eval '(term-keys/xterm-run-emacs)'
See the term-keys/xterm-* definitions for more xterm-specific help code.
You may also want to disable the eightBitInput xterm option, e.g. with -xrm 'XTerm*eightBitInput: false'.
kitty
kitty is configured via its kitty.conf configuration file.
To configure kitty for term-keys, use term-keys/kitty-conf to generate a kitty.conf fragment:
(require 'term-keys-kitty)
(with-temp-buffer
(insert (term-keys/kitty-conf))
(write-region (point-min) (point-max) "~/kitty-for-term-keys.conf"))
Then, add the output to your main kitty.conf file.
You can customize kitty's mapping of GLFW modifiers to Emacs modifiers in the generated configuration using the respective customize group, i.e.: M-:(progn (require 'term-keys-kitty) (customize-group 'term-keys/glfw))
wezterm
wezterm is configured via its wezterm.lua configuration file.
To configure wezterm for term-keys, use term-keys/wezterm-conf to generate a wezterm.lua fragment:
(require 'term-keys-wezterm)
(with-temp-buffer
(insert (term-keys/wezterm-conf))
(write-region (point-min) (point-max) "~/wezterm-for-term-keys.lua"))
Then, add the output to your main wezterm.lua file.
You can customize wezterm's mapping of GLFW modifiers to Emacs modifiers in the generated configuration using the respective customize group, i.e.: M-:(progn (require 'term-keys-wezterm) (customize-group 'term-keys/glfw))
Alacritty
Alacritty is configured via its alacritty.yml configuration file.
To configure alacritty for term-keys, use term-keys/alacritty-config to generate a alacritty.yml fragment:
(require 'term-keys-alacritty)
(with-temp-buffer
(insert (term-keys/alacritty-config))
(write-region (point-min) (point-max) "~/alacritty-for-term-keys.yml"))
Then, add the output to your main alacritty.yml file.
You can customize Alacritty's mapping of its supported modifiers to Emacs modifiers in the generated configuration using the respective customize group, i.e.: M-:(progn (require 'term-keys-alacritty) (customize-group 'term-keys/alacritty))
Konsole
Konsole provides an interface for adding new and editing existing escape sequences for key combinations
(Settings → Edit current profile... → Keyboard → Edit).
term-keys can generate a Konsole keyboard profile according to its settings.
To do so:
-
Create a new keyboard profile, based on the default one.
(Select "Default (XFree 4)", and click "New...".)
Name it e.g. Emacs.
-
Append the output of term-keys/konsole-keytab to the newly created .keytab file, e.g.:
(require 'term-keys-konsole)
(with-temp-buffer
(insert (term-keys/konsole-keytab))
(append-to-file (point-min) (point-max) "~/.local/share/konsole/Emacs.keytab"))
-
Assign the keyboard profile to a new or existing Konsole profile.
You can customize the mapping of Konsole (Qt) modifiers to Emacs modifiers using the respective customize group, i.e.: M-:(progn (require 'term-keys-konsole) (customize-group 'term-keys/konsole))
You may also want to disable some default Konsole shortcuts (Settings → Configure Shortcuts...),
as they may interfere with standard Emacs commands.
Yakuake
Yakuake seems to share much with Konsole, and can be configured in the same way.
See the section above for details.
Linux console
The Linux console can be customized using .keymap files and the loadkeys program.
You can configure it for term-keys as follows:
-
Use term-keys/linux-keymap to create a .keymap file:
(require 'term-keys-linux)
(with-temp-buffer
(insert (term-keys/linux-keymap))
(write-region (point-min) (point-max) "~/term-keys.keymap"))
-
Load the created .keymap file:
$ sudo loadkeys ~/term-keys.keymap