Getting Xcape functionality in Wayland

The thing I’ve missed most since switching to Wayland is definitely Xcape, but more on that later. The solution is to use the Interception Tools plugin caps2esc and in fact this is a display-server agnostic solution because it’s at a much lower level, so it also works for X11 and even in the virtual consoles! 😮

Luckily Arch Linux actually has this plugin in the official package repos, so installation is easy:

$ pacman -S interception-caps2esc

This also installs interception-tools as a dependency, including udevmon which is a program that associates a job with any current and new devices (e.g. keyboards). In our case the “job” we want is a shell script piping intercepted keyboard output through the caps2esc plugin.

To do this, you copy this snippet, literally as-is from the plugin README file:

- JOB: intercept -g $DEVNODE | caps2esc | uinput -d $DEVNODE
  DEVICE:
    EVENTS:
      EV_KEY: [KEY_CAPSLOCK, KEY_ESC]

Paste that into a new file under udevmon.d, for example /etc/interception/udevmon.d/caps2esc.yaml to associate this plugin with the CapsLock and Esc keys on all devices.

Lastly, start up the udevmon service. Despite the README talking about running commands manually, the Arch Linux package includes a systemd unit file with the recommend defaults pre-configured, you just need to turn it on:

sudo systemctl enable --now udevmon.service

That’s it! Now Esc and CapsLock are swapped and holding down Esc functions as Ctlr, in Wayland, in X and even in the TTY!!!


So… why did I want to do this? Well, the caps2esc project documentation says it best:

transforming the most useless key ever in the most useful one

I spend a lot of my computer-time editing code and text in Vim where the Esc key is really frequently used because it’s how you change to “normal mode”. Lots of other programs also let you use Escape to do things like dismissing windows or stepping out of form fields. In GNOME settings there’s an option to swap the CapsLock and Escape keys and that made it a much smaller reach to get the Esc key with my pinky.

Reading up a bit more on this I stumbled upon Xcape which lets you set up CapsLock so that it’ll send Esc if you press it briefly, but act as Ctrl if you hold it down together with other keys. Amazing. The Control key is another annoying pinky-reach and its position varies between my work and personal computers. It’s used in loads of keyboard shortcuts across all apps, and it’s part of most key combinations in readline shortcuts used in the shell. As a power user, having both this and Escape close at hand on this really big, easy-to-hit CapsLock key in the home row is fantastic! Until I switched to macOS.

The old work laptop got replaced with a newer MacBook that brought exciting new hardware compatibility issues. In this case, the built-in keyboard refused to work at all, and after a few weekends of trying to fix it I gave up and accepted the challenge of using the other OS. I was happy to learn this dual Ctrl-Esc functionality was something Mac users were after too and using Karabiner-Elements I was able to set this up pretty easily, as well as the compose key. I ended up using this for a several years and it worked well. I might write a bit about my personal experiences with macOS compared to Linux at some point but eventually this laptop got old too and the next machine was Linux-compatible.

Meanwhile in Linux-land support for Wayland is stable with GNOME desktop and so I switched from X11 to Wayland to get better scaling between the laptop’s HiDPI screen and the HD external monitor. The issue of being able to share my desktop got fixed upstream but the last thing I was missing from X11 was some alternative to Xcape. The X in Xcape alludes to X11, and if an X server isn’t running Xcape can’t start and anyway there wouldn’t be any X input events to handle without X. Pretty annoying. I tolerated this for about a year because the solutions I found looked a bit less out-of-the box than Xcape and I didn’t want to spend so much time tweaking a computer. Xcape issue #67 wayland support lists some alternatives, ranging between pretty hacky to “just use X”.

It was in this list that I came across caps2esc, bookmarked it for later, and later is today. I’ll be honest, I struggled a bit to make heads or tails of the Interception Tools documentation. On one hand it seems the way to write custom “plugin” functionality was written in C, and the code looked short and readable enough, but on the other hand it’s talking about configuring things with YAML files. The YAML config didn’t seem to be input to the plugin itself though. After some back-and-forth between the main docs and the plugin docs it became obvious: The plugin itself is written in C. If you have it installed, you can use the interception tools intercept and udevmon to send keyboard events through the caps2esc plugin. Using udevmon you can configure to which devices to apply this rule (called a job) and the way to do that is to write some udevmon config in YAML. The puzzle pieces all fit together and the result is the condensed set of instructions at the top of this post.

Fixing a typo across multiple repos

Yesterday I found a typo in a pull request description while browsing another team’s project which I stumbled upon. I mentioned it to the author but it turned out that that part of the text came from the repository’s pull request template, which means every pull request will have this amusing but irritating mistake. I sent them a pull request, modifying the template, to fix the mistake at the source and avoid it in future, and thought that would be the end of it.

It turns out that template was written once and then copied across to new repos, which means this typo actually exists in almost all the pull requests in all of that team’s projects. Well that escalated quickly. This is the point where the average person probably says “OK whatever, it’s not worth it for something so small, there are too many repos, it’s just a small typo, never mind” and stop. A very determined person might actually start opening browser tabs and psyching themselves up to do pull requests. I open my terminal emulator and start writing a for loop. Continue reading

Use HOME as your GOPATH

How and why I normalised my Go paths and personal/local home paths.

Like many people, I have my own scripts and stuff in a bin directory in my home directory. Actually it’s a symlink to ~/.local/bin because I saw there was a ~/.local/share which some programs use to store user-specific things and I wanted to be consistent.

Then I saw some people have ~/bin and ~/src and I thought that looked like a Continue reading

Vim “fake fullscreen”: open split windows in a new tab

Since I use a lot of split windows in Vim, for example when exploring the git log or editing closely related files, a pattern I noticed is I often want to make one of the smaller windows full screen momentarily so I can read more at once without scrolling and then close it when I’m done. I made a really simple mapping to simulate this “full screen” idea:

:nnoremap <Leader>f :tabe %<CR>

This opens the current window’s buffer in a new tab (fake full screen ?) and when I close it I’m back to tab one with my split windows.

To demonstrate, here’s a gif in which I inspect the git blame for a file, open a patch and then open it “full screen” in a new tab:

Vim fake fullscreen demo gif

Vim fake fullscreen demo gif


Browsing the git log isn’t the best example because fugitive’s blame window already has an O mapping which opens the patch in a tab instead of a split and the necessity for this would be clearer with bigger files like those I edit at work.

This is one of the few things I use tabs for since I’m mostly jumping through buffers. Hopefully it’s useful for you too!