I use i3 as my window manager so I more or less always know which workspace programs run in. Switching to a program is therefore as simple as going to a specific workspace. However, sometimes I end up with a lot of windows scattered in various workspaces and scratchpad and it would be nice to have a quick way to bring a particular program into focus.

To do that, I put together a small shell script that finds all windows and pipes them to dmenu for user selection and I have a keybinding to conveniently invoke the script. I believe this functionality is available in rofi but I was looking for something more minimal (and I prefer dmenu over rofi). This is the script in its entirety:

#!/bin/env bash

set -e

windows=$(i3-msg -t get_tree | jq '.. | objects |
    select(.window_type == "normal") |
    {name: (.window_properties.class + ": " + .name), id: .id}')

selected_window=$(echo "$windows" | jq -r '.name' | dmenu)
export selected_window

window_id=$(echo "$windows" | jq 'select(.name==env.selected_window) | .id') && i3-msg "[con_id=$window_id] focus"

The only dependency is jq to parse the i3 window tree in JSON.