The problem

After a few years using Windows 10 as my main OS, I went back to using Ubuntu1.

Under Windows, I’m using AutoHotkey to define a bunch of custom keyboard shortcuts to switch between different program windows.

I usually bind a single key to focus to a program window or launch it if not already running.

For example, for Google Chrome:

IfWinExist ahk_exe chrome.exe
	WinGet, id, list, ahk_exe chrome.exe
	Loop, %id%
	    this_id := id%A_Index%
		WinActivate, ahk_id %this_id%
	Run C:\Program Files\Google\Chrome\Application\chrome.exe
	WinWait ahk_exe chrome.exe

Under Linux, I managed to have a similar feature but at the time I was using xmonad.

Now, I’m sticking (for now) with the default DE that is GNOME 3.

Much to my disappointment, the latter does not support out of the box defining shortcuts with this “focus or launch” behavior.

The solution

I googled around, but didn’t find any solution that satisfied my needs.

Specifically, I wanted:

At first I tried using AutoKey but found it kinda unstable.

Ultimately I ended up with a small script relying on wmctrl and xdtotool.

wmctrl is used to determine the list of windows for current desktop.

xdotool is used to list the windows for a given class.

I used python for the convenience of parsing and manipulating lists.

p3r7/focus-or-launch - GitHub



It works as long as we’re running X server.

But we all know that we’ll ultimately switch to Wayland.

When that happens, we’ll need another solution.


  1. I still dual boot as there are several programs I rely on, such as Capture One… 

  2. This is necessary for launching stuff with arguments or that would spawn programs with a different name (e.g. emacsclient spawning emacs). 

Tagged #linux, #productivity.