Remapping ESC, Grave(`), and Tilde(~) in Sway/Wayland

17 October 2021

I recently revisited the idea of using a 65% keyboard with the purchase of a Keychon K6. The setback back then that turned me away from this form factor was the lack of dedicated grave-tilde key (`~), and as a programmer and heavy terminal user that was a deal breaker to me. Turns out it is pretty easy to remap it in Linux (Sway/Wayland).

Keychron K6

Capslock -> ESC , ESC -> `~

65%/60% keyboards omit the function key row entirely; to retain a dedicated ESC key, lots of them1 put ESC on the main layer of the original `~ key; one would then type ` and ~ via modifiers (e.g. Fn).

The most commonly suggested solution to resurrect `~ is to map Capslock as ESC, and then map ESC back to its original `~ designation.

Sway: Capslock -> ESC

This is easy and it is alsmost mentioned in every other Sway setup tutorial. In your sway config:

input * {
    xkb_options caps:escape
}

Sway: ESC -> `~

Mapping ESC to `~ is slightly trickier as there seems to be no out-of-the-box configurations for this kind of remapping. This can be done however via a custom XKB laytout2 3. We would create a custom laytout that “inherits” a standard layout (US in our case), and add custom key mappings on top.

$ cat ~/.xkb/symbols/65-percent-escape-tilde-grave
default partial alphanumeric_keys
xkb_symbols "basic" {
	include "us(basic)"

	name[Group1] = "English (US, 65/60% escape grave tilde)";
    key <ESC>  {[ grave,    asciitilde      ]       };
};

In your Sway config, add:

input 1452:591:Keychron_Keychron_K6 xkb_layout "65-percent-escape-tilde-grave" 

Save and reload your Sway config.

? linux
# mechanical keyboard sway esc tilde grave