Hacking i3: Window Promoting

(Day 16 of 30 Days of Blogging)

One of the only things I miss from when I used xmonad many years ago was being able to hit a keybind to swap the currently focused window with the “master” window. I think the default keybind for that in xmonad is alt-Enter.

i3 doesn’t have the concept of a master window, but if we consider the master window to just be the largest window, the same effect can be achieved:

Simple, but often useful. Here’s the promote-window script that implements it:

#!/usr/bin/env python3
#
# Promotes the focused window by swapping it with the largest window.

from i3ipc import Connection, Event

def find_biggest_window (container):
    max_leaf = None
    max_area = 0
    for leaf in container.leaves():
        rect = leaf.rect
        area = rect.width * rect.height
        if not leaf.focused and area > max_area:
            max_area = area
            max_leaf = leaf
    return max_leaf

i3 = Connection()

for reply in i3.get_workspaces():
    if reply.focused:
        workspace = i3.get_tree().find_by_id(reply.ipc_data["id"])
        master = find_biggest_window(workspace)
        i3.command("swap container with con_id %s" % master.id)

Make sure you have python3 with i3ipc, and then add this your i3 config:

bindsym $mod+p exec --no-startup-id ~/.config/i3/promote-window

No big-brain Haskell required! If you read this far and found this interesting you might also like Hacking i3: Window Swallowing.