Johan wrote this
@ 2022-07-06

Quake kitty

I like my software boring and stable. I try to keep myself patched and all that, but I don’t really trust updates. That said, sometimes you gotta mix it up a little.

I’ve been running Linux for a couple of years now on my main development machine. I have very few issues with it. It’s ridiculously fast, it can be customized however deeply I want, and above all: I don’t have to worry about my OS slowly merging with my mobile phone and turning my digital life into a dismal panopticon in order to package me up and sell me.

I try to keep my Ubuntu as close to default settings as possible ‒ working against the grain can be exhausting ‒ but there are two apps I use so much that they deserve special attention: my code editor and my terminal. I have a fairly custom Sublime Text config (kept in a dotfiles repository) and I’ve been using guake as a drop-in terminal replacement for the last two years or so. It borrows inspiration from the old Quake terminal in that you hit F12 and WHAM you get a full-screen terminal with a bit of opacity on top of your windows. Hit F12 again and your terminal slinks away, focusing back to wherever you were before. It’s embedded in my muscle memory now.

Since Ubuntu moved to Wayland, though, the F12 button has been kind of finicky. Depending on which app has focus, the hotkey can’t be relied on anymore. The worst offender is Pop: if I’m pairing with someone I’ll have to tap three or four times for anything to happen which, as I’m sure you can imagine, is amazing for productivity. The death blow for Guake came after I recently switched my code font to Fira Code: naturally I wanted to switch my terminal font as well, and it turns out the native Gnome VTE (which Guake uses for rendering) doesn’t support ligatures. MY FANCY ARROWS NOOOO

I hit the ol’ series of tubes to see what else was out there and after some cursory research I installed kitty instead. It seems robust and performant, and has good support for Unicode and ligatures. But I don’t want to alt-tab around, I want my F12 hotkey! I toyed around with the kitty configuration and startup arguments, but they weren’t really enough. Not to worry, though ‒ I’m a programmer, remember? After an hour of fiddling I came up with a bash script using wmctrl that does what I want:

#!/bin/bash
ACTIVE_WINDOW=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW|cut -f 2)
ACTIVE_WINDOW_CLASS=$(xprop -id $ACTIVE_WINDOW WM_CLASS|tr -d ,\"|awk '{print $3};')

if [ $ACTIVE_WINDOW_CLASS = "kitty" ]; then
  wmctrl -ia $(cat ~/.lastfocusedwindow)
else
  echo $ACTIVE_WINDOW > ~/.lastfocusedwindow
  wmctrl -xa kitty.kitty || ~/.local/kitty.app/bin/kitty
fi

It’s fairly straightforward: the script looks at the currently focused window to see if it’s kitty. If it isn’t, save the currently focused window id to a file called .lastfocusedwindow and then either spawn or focus the kitty window. If it is kitty, bring up the .lastfocusedwindow instead, sending kitty to the background. Bind the F12 button to this script, and enjoy!