Use ‘xkb’ to setup full-spectrum of modifiers—Meta, Alt, Super, and Hyper—for use with Emacs


Few words about Meta, Super, Hyper and Alt modifiers on Emacs

Emacs traditionally allowed (and continues to allow use of) Super, Hyper and Alt modifiers. Unfortunately, modern keyboards do not come with full spectrum of modifiers that Emacs understands.

For example, I use a Dell Windows Laptop. It runs Debian/Unstable configured with a GNOME/Flashback Desktop. I get Super and Meta modifiers out of the box. However there is no reliable way to configure Hyper and Alt modifiers.

Wouldn’t it be wonderful if you could configure your keyboard to generate the full spectrum of modifiers?

The entry Modifier Keys (GNU Emacs Manual) says,

Although the <Control> and <Meta> modifier keys are commonly used, Emacs supports three other modifier keys. These are called <Super>, <Hyper>, and <Alt>.

Few terminals provide ways to use these modifiers; the key labeled <Alt> on most keyboards usually issues the <Meta> modifier, not <Alt>.

The standard key bindings in Emacs do not include any characters with the <Super> and <Hyper> modifiers, and only a small number of standard key bindings use <Alt>.

The modifier keys recognized by Emacs, were available on keyboards found on original Lisp Machines used in MIT.

Space Cadet Keyboard

The Space-cadet keyboard says,

The space-cadet keyboard is a keyboard designed by John L. Kulp in 1978 and used on Lisp machines at Massachusetts Institute of Technology (MIT).

The space-cadet keyboard was equipped with 7 modifier keys:

  • 4 keys for bucky bits: Control, Meta, Super, and Hyper and
  • 3 shift keys, called Shift, Top, and Front

Each group was in a row, thus allowing easy chording, or pressing of several modifier keys;

For example, Control + Meta + Hyper + Super could be pressed with the fingers of one hand, while the other hand pressed another key.

By combining the modifier keys, it is possible to make a vast array of inputs, Many users were willing to memorise the command meanings of so many characters, as it reduced typing time. This attitude shaped the interface of Emacs;

With the passage of time, many keboard manufacturers thought that so many keys were excessive and objected to this design on the grounds that such a keyboard can be difficult to operate.

Very few modifier keys survive to this day.

In modern times, when Emacs was ported to PCs, the Alt key was used in place of Meta, and Emacs continutes to designate Alt key presses with M- prefix.

This article will outline how to use X Keyboard Extension (xkb) to setup additional modifier keys for use within Emacs.

All methods of configuring your keyboard is unsatisfactory; they are ancient, confusing or fall short of Emacs ideals

A word about gnome-tweaks

The modern method of configuring your Keyboard is through the gnome-tweaks utility of the GNOME DE. Unfortunately, none of the standard tweaks provide a way to configure your keyboard in a manner Emacs expects it.

For example, under gnome-tweaks -> Keyboard & Mouse -> Additional Layout Options -> Caps Lock behaviour, there is an option to Make Caps Lock an additional Hyper. When you turn on this option, GNOME DE sets up Hyper and Super keys on to the same modifier bit. This overloading of the same modifier bit, for two different modifier keys confuses Emacs infinitely. Configure this option, and see for yourself, what I mean here.

Configuring keyboard with gnome-tweaks

A word about xmodmap

The ancient, and now long obsolescent, but still accepted means of setting up the modifier keys is through the xmodmap.

Those of you who have taken the xmodmap route, would have seen a spectrum of suggestions, with no consistent thread uniting all of them. The suggestions you gathered would have been t(a)inted by the personal situation / needs / fancies of the narrator. At the end of the journey, you would have felt exhausted, grappling with an uneasy feeling that you cannot persuade yourself to trust any of those suggestions.

An XKB configuration for getting full spectrum of Emacs modifier keys

Amid the cesspool of alternative narratives to setup a full spectrum of Emacs modifier keys, I have not come across a single suggestion which uses xkb: the X Keyboard Extension. This article endeavours to introduce xkb as a modern, user-friendly means to obtain all of the modifier keys— Alt, Hyper, Meta and Super —for use with Emacs.

Some simplifying assumptions about your language / keyboard needs

A keyboard configuration by its very nature is intimately tied to human languages and culture. So, it is wrongful to talk of a keyboard configuration without also talking about the users language needs.

For the sake of this article, I have made the following simplifying assumptions, which is true in my case:

  1. You use a standard English/US QWERTY PC keyboard
  2. You have no need to input other languages; that is, you don’t use different keyboard layouts and/or shift between different levels. Technically, this means that you aren’t using the -option command line option in your setxkbmap settings.

The assumption (2) may come across as particularly restricive, and impractical. But it is not so.

Emacs comes with a rich set of input methods, and when you are within Emacs you don’t have to use your Desktop or OS provided means to input characters in other languages. For example, when editing text in Tamil, I use Tamil Phonetic Input Method in Emacs, instead of the OS provided Tamil-input mechanism.

STEP 1: Define an autostart application to configure your keyboard

Create .config/autostart/emacs-xkb.desktop with following content

~/.config/autostart/emacs-xkb.desktop

[Desktop Entry]
Type=Application
Exec=sh -c 'xkbcomp -I/home/emacksnotes/.config/xkb /home/emacksnotes/.config/xkb/us-emacs.xkb $DISPLAY'
Terminal=true
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=Emacs Keyboard
Name=Emacs Keyboard

The above desktop file, sets up /.config/xkb/us-emacs.xkb as the keymap source file. This keymap source file will use definitions from files in the include directory ./config/xkb.

STEP 2: Define the keymap source file

Create a .config/xkb/us-emacs.xkb with the following content

~/.config/xkb/us-emacs.xkb

xkb_keymap {
	xkb_keycodes  { include "evdev+aliases(qwerty)"	};
	xkb_types     { include "complete"	};
	xkb_compat    { include "complete"	};
	xkb_symbols   { include "pc+us+emacs(default)"	};
	xkb_geometry  { include "pc(pc105)"	};
};

The above keymap uses the standard US QWERTY layout, with additional modifications as suggested by emacs(default).

STEP 3: Setup key definitions for use with Emacs

(This is the meatiest part of the article; it is my original contribution)

Create a .config/xkb/symbols/emacs with following content

~/.config/xkb/symbols/emacs

default partial modifier_keys

// Default Configuration
// - RALT generates Alt
// - LALT generates Meta
// - LWIN generates Super
// - CAPS generates Hyper
xkb_symbols "default" {
  include "emacs(alt_ralt)"
    include "emacs(meta_lalt)"
    include "emacs(super_lwin)"
    include "emacs(hyper_caps)"
    include "emacs(modifier_mapping)"
    };

// ALT
partial modifier_keys
xkb_symbols "alt_ralt" {
  key <RALT> { [Alt_L, Alt_R] };
  include "emacs(modifier_mapping)"
};

// META
partial modifier_keys
xkb_symbols "meta_lalt" {
  key <LALT> { [Meta_L, Meta_R] };
  include "emacs(modifier_mapping)"
};

// SUPER
partial modifier_keys
xkb_symbols "super_lwin" {
  key <LWIN> { [Super_L, Super_R] };
  include "emacs(modifier_mapping)"
};

// HYPER
partial modifier_keys
xkb_symbols "hyper_caps" {
  key <CAPS> { [Hyper_L]  };
  include "emacs(modifier_mapping)"
};

partial modifier_keys
xkb_symbols "modifier_mapping" {
  modifier_map Mod1     { <ALT>         , Alt_L         , Alt_R         };
  modifier_map Mod2     { <META>        , Meta_L        , Meta_R        };
  modifier_map Mod3     { <SUPR>        , Super_L       , Super_R       };
  modifier_map Mod4     { <HYPR>        , Hyper_L       , Hyper_R       };
};

// None
partial modifier_keys
xkb_symbols "none" {

};

// Local Variables:
// mode: c
// End:

Final Destination: A look at your xmodmap

The previous steps, sets up the modifiers keys as follows

xmodmap as installed by this article

$ xmodmap

xmodmap:  up to 3 keys per modifier, (keycodes in parentheses):

shift       Shift_L (0x32),  Shift_R (0x3e)
lock
control     Control_L (0x25),  Control_R (0x69)
mod1        Alt_L (0x6c),  Alt_L (0xcc)
mod2        Meta_L (0x40),  Num_Lock (0x4d),  Meta_L (0xcd)
mod3        Super_L (0x85),  Super_R (0x86),  Super_L (0xce)
mod4        Hyper_L (0x42),  Hyper_L (0xcf)
mod5        ISO_Level3_Shift (0x5c),  Mode_switch (0xcb)

Modifier mappings as installed by this article

Name on the Keyboard Keysym X11 Modifier Comment
Left Ctrl Control control Unconfigured; Left at its default
Right Ctrl
Right Alt Alt Mod1
Left Alt Meta Mod2
Left Windows Super Mod3
Right Windows (unconfigured) (unconfigured) No Right Win Key on my Laptop
CapsLock Hyper Mod4

Note that there is a one-to-one mapping between the keysym, and the X11 modifier. This one-to-one mapping is essential for Emacs to recognize each of the modifiers uniquely, and not confuse between them.

It is worthwhile, comparing the xmodmap output above with the default xmodmap setup by the GNOME Desktop

xmodmap as installed by GNOME Desktop

~$ setxkbmap -layout us

~$ xmodmap
xmodmap:  up to 4 keys per modifier, (keycodes in parentheses):

shift       Shift_L (0x32),  Shift_R (0x3e)
lock        Caps_Lock (0x42)
control     Control_L (0x25),  Control_R (0x69)
mod1        Alt_L (0x40),  Alt_R (0x6c),  Meta_L (0xcd)
mod2        Num_Lock (0x4d)
mod3
mod4        Super_L (0x85),  Super_R (0x86),  Super_L (0xce),  Hyper_L (0xcf)
mod5        ISO_Level3_Shift (0x5c),  Mode_switch (0xcb)

Modifier mappings as installed by GNOME Desktop

Name on the Keyboard Keysym X11 Modifier Comment
Left Ctrl Control control Unconfigured; Left at its default
Right Ctrl
Left Alt, Right Alt Alt Mod1
Left Alt (Shifted), Right Alt (Shifted) Meta Mod1
Mod1 is overloaded.

Emacs resolves the modifier in favor of Meta

Left Windows Super Mod4
Right Windows (unconfigured) (unconfigured) No Right Win Key on my Laptop
Hyper Mod4 Not mapped to any physical key.
If Caps Lock were mapped to Hyper, Mod4 will be overloaded.

Emacs doesn’t resolve conflicts between Hyper and Super modifiers (as it does for Meta and Altmodifiers). This results in some confusion.

STEP 4: Log out and re-login to GNOME Desktop

Once you logout and login to your GNOME Destop, momentarily you will notice the terminal window that runs the autostart script.

Once the autostart script exits, you can check your modifier map. It would be as outlined in the preceding section.

You need not logout and login again to install the new keymap.

At any time, you can use the Exec line in the emacs-xkb.desktop file to install the fresh set of key definitions that are conducive for use with Emacs.

When you run the Exec command, this is what your will see.

$ sh -c 'xkbcomp -I/home/emacksnotes/.config/xkb /home/emacksnotes/.config/xkb/us-emacs.xkb $DISPLAY'
Keycodes above 256 (e.g. <I256>) are not supported by X and are ignored
Error:            Super_R added to symbol map for multiple modifiers
                  Using Mod3, ignoring Mod4.
Error:            Super_L added to symbol map for multiple modifiers
                  Using Mod3, ignoring Mod4.
Error:            Key <SUPR> added to map for multiple modifiers
                  Using Mod3, ignoring Mod4.
Error:            Meta_R added to symbol map for multiple modifiers
                  Using Mod2, ignoring Mod1.
Error:            Meta_L added to symbol map for multiple modifiers
                  Using Mod2, ignoring Mod1.
Error:            Key <META> added to map for multiple modifiers
                  Using Mod2, ignoring Mod1.
No symbols defined for <AB11> (keycode 97)
.....

The Error-s that you see above are essential or innocuous. You can ignore them.

Conclusion

This article suggests a xkb configuration to setup a full spectrum of Emacs Modifier Keys.

The suggestion here is a good starting point for introducing a set of Emacs-specific options to the stock xkb: the X Keyboard Extension.

As an avid Emacs user, my hope is that in the very distant future, the suggestions here would inspire someone to introduce Emacs-specific options to the standard gnome-tweaks menu.

Even though I don’t understand much of xkb infrastructure, I can say with full-confidence that existing set of keyboard optons in gnome-tweaks are thoroughly unfit for producing a keyboard layour that is well-suited to the needs of an Emacs user.

Before I sign off … I have more things to say about using xkb to get a full-spectrum of Emacs modifier keys.

I have planned the xkb articles in three parts. This article is the meatier part, and introduced a way to go about configuring Emacs modifiers.

The other two article will talk about

  • How to setup IBus to configure three layouts: (a) US PC, (b) US PC with Emacs Modifiers, and (c) Tamil (or for that matter any input method that comes with IBus), and switch between them.
  • Some very essential preliminaries that will help you dig deep in to your own (or someone else) xkb configuration. This proposed article will not make you an expert of xkb, but it will help you work with your own (or others) xkb definitions, and make it easy for you to identify the problem areas when it comes to the modifier keys.

Talk to you soon …

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close