--- layout: default title: Docs group: Docs ---

User-contributed article: Swapping workspaces

If you have workspace 1 on one monitor and workspace 2 on another monitor and want to quickly swap the workspaces among the monitors, you can use i3's IPC mechanisms to do it. Here's how:

i3 already includes a way to move an individual workspace from one monitor to another. But what we want to achieve is the simultaneous movement of workspaces between the monitors. To do this, we can write a script that detects the currently active workspace on each monitor and then moves that workspace to the other monitor.

The script uses i3's IPC via the i3-py Python bindings.

#!/usr/bin/python2.7

import i3
# retrieve only active outputs
outputs = filter(lambda output: output['active'], i3.get_outputs())

# set current workspace to output 0
i3.workspace(outputs[0]['current_workspace'])

# ..and move it to the other output.
# outputs wrap, so the right of the right is left ;)
i3.command('move', 'workspace to output right')

# rinse and repeat
i3.workspace(outputs[1]['current_workspace'])
i3.command('move', 'workspace to output right')

A very simple way to use this script is as follows: Put the script in a file, named for example switch.py, and put switch.py and i3.py (downloaded from the Python bindings site) in the same folder. I typically put it in $HOME/.i3/, the same directory which contains i3's config file. Next, put a keybinding like

bindsym $mod+Shift+s exec /home/username/.i3/switch.py

in i3's config file and restart i3 in place. The next time you press mod+Shift+s, your workspaces will be swapped among your monitors.

Author: Sagar Behere