The i3 FAQ has migrated to https://www.reddit.com/r/i3wm/. All content here is read-only.

# How can I sort windows inside a container automatically?

I am using clusterssh to manage a large number of systems. clusterssh opens a terminal for each host plus an input window. The problem with that is that the windows are not created in the order specified on the command line, but in a rather random order. That is:

\$ cssh server1 server2 server3


may very well create this layout:

+---------------------+
| server3             |
+---------------------+
| server1             |
+---------------------+
| server2             |
+---------------------+
| input               |
+---------------------+


While not really a problem with only three windows, trying to find the right window out of 40 is rather cumbersome. And manually sorting them even more so.

(Ironically, on floating window managers CSSH can create a tiled and sorted layout of terminal windows)

So the question is: is there an easy way to sort all windows in a container by name?

I know, I can get the layout tree from the IPC, but there seems to be no way to modify it other than using the commands available for key bindings (focus, move,...).

edit retag close merge delete

Sort by » oldest newest most voted

Here is a solution I came up with using Python, ziberna's i3-library

#!/usr/bin/env python

parentContainer = i3.parent(currentWindow['id'])
if parentContainer['layout'] in ['splitv', 'stacked']:
direction = 'up'
else:
direction = 'left'

nodes = parentContainer['nodes']

for i in range(len(nodes)):
for j in range(i):
moves = 0
if nodes[i]['name'] < nodes[j]['name']:
moves += 1
if moves:
i3.focus(con_id=nodes[i]['id'])
for m in range(moves):
i3.move(direction)

i3.focus(con_id=currentWindow['id'])


Compared with Bubble-sort this cuts down on the refocusing needed, as each window is only focused once for sorting.

While it works, I'm still not really happy with it:

• Moving a lot of windows, each multiple times, takes seconds (depending on the initial order)
• Anything that changes focus during the sorting, will prevent proper sorting and could even lead to hang-ups (could be at least partially prevented if criteria could be used with move) non-adjecent windows.

This algorithm (like Bubble-sort) already does only the minimum number of move operations for in-place sorting (see here for some theory on that).

This means that for any real improvements, the windows would probably have to be moved elsewhere for sorting and than moved back.

I tried sorting by moving the windows temporarily to scratchpad or another workspace. While this needs on average far less move commands, it needs even more time. I guess the problem is, that the windows are resized on every move.

Anyway, here is the code for the scratchpad version for reference.

#!/usr/bin/env python

import i3

currentWindow = i3.filter(focused=True)[0]

parentContainer = i3.parent(currentWindow['id'])
if parentContainer['layout'] in ['splitv', 'stacked']:
direction = 'down'
else:
direction = 'right'

nodes = parentContainer['nodes']

snodes = sorted(nodes, key=lambda n:n['name'])

for node in snodes[1:]:
i3.focus(con_id=node['id'])

i3.focus(con_id=nodes[0]['id'])

for node in snodes[1:]:
i3.focus(con_id=node['id'])
i3.floating('disable')

i3.focus(con_id=currentWindow['id'])


The workspace version is similar, but slower and confuses with switches to another workspace during sorting.

more

I like it :) One obvious improvement would be to make the sort internally in python, and execute the focus and move commands afterwards. You can select the windows for sorting based on their window name, or you could mark them for easy focusing.

( 2014-01-09 18:39:54 +0000 )edit

As i3 is lacking the ability to just put windows where they need to be, sorting before moving does not really help. Also it is moving and focusing that takes most of the time. Bubble-sort is already doing the minimum amount of swapping/moving, so I can only cut back on the focusing needed.

( 2014-01-10 11:27:07 +0000 )edit

## Stats

Seen: 372 times

Last updated: Jan 10 '14