#!/usr/bin/env python3

import i3ipc

# make connection to i3 ipc 
i3 = i3ipc.Connection()

# check if workspaces are all in order
def workspaces_ordered(i3conn):
    last_workspace_number = 0
    for i in sorted(i3conn.get_workspaces(), key=lambda x: x['num']):
        if i['num'] != last_workspace_number+1:
            return False
        last_workspace_number += 1
    return True

# find all the workspaces that are out of order and 
# the least possible valid workspace number that is unassigned
def find_disordered(i3conn):
    last_workspace_number = 0
    disordered = []
    least_number = None
    for i in sorted(i3conn.get_workspaces(), key=lambda x: x['num']):
        if i['num'] != last_workspace_number+1:
            disordered.append(i['num'])
            if least_number is None:
                least_number = last_workspace_number + 1
        last_workspace_number += 1
    return (disordered, least_number)

# renumber all the workspaces that appear out of order from the others
def fix_ordering(i3conn):
    if workspaces_ordered(i3conn):
        return
    else:
        workspaces = i3conn.get_tree().workspaces()
        disordered_workspaces,least_number = find_disordered(i3conn)
        containers = list(filter(lambda x: x.num in disordered_workspaces, workspaces))
        for c in containers:
            for i in c.leaves():
                i.command("move container to workspace %s" % least_number)
            least_number += 1
    return

# callback for when workspace focus changes
def on_workspace_focus(i3conn, e):
    fix_ordering(i3conn)


if __name__ == '__main__':
    i3.on('workspace::focus', on_workspace_focus)
    i3.main()


Add script that makes sure workspace numbers are always in consecutive order by moving windows on out of order workspaces to the appropriate workspace
Add script that makes sure workspace numbers are always in consecutive
order by moving windows on out of order workspaces to the appropriate
workspace
