Ubuntu – How to automatically add workspaces, only if I need them


Assume I am using 4 workspaces and I incidentally need more, is there an automated process or, if impossible, an easy way to incidentally add more workspaces (instead of installing Ubuntu tweak etc. etc.).

Best Answer

Automatically set the number of workspaces; add and remove columns and rows, depending on your needs

Below a version of a (the) backround script that will automatically add workspaces if you entered the last column or row of your workspace-matrix.

This is how it works:

  1. If you arrive at the last column or row, additional viewports are added:

    enter image description here

  2. If your workspaces are unused for 5-10 seconds and there are no windows on it, the additional workspaces will be removed again. You will however always keep one extra row below, and one extra column right of your current viewport:

    enter image description here

The script:

#!/usr/bin/env python3
import subprocess
import time
import math

# --- set default workspaces below (horizontally, vertically)
hsize = 2
vsize = 2
# --- set the maximum number of workspaces below
max_ws = 10

def set_workspaces(size, axis):
        "dconf", "write", "/org/compiz/profiles/unity/plugins/core/"+axis,

def get_res():
    resdata = subprocess.check_output(["xrandr"]).decode("utf-8").split()
    curr = resdata.index("current")
    return (int(resdata[curr+1]), int(resdata[curr+3].replace(",", "")))

def wspace():
        sp = subprocess.check_output(["wmctrl", "-d"]).decode("utf-8").split()
        return ([int(n) for n in sp[3].split("x")],
                [int(n) for n in sp[5].split(",")])

    except subprocess.CalledProcessError:

def clean_up(curr_col, curr_row):
        w_list = [l.split() for l in subprocess.check_output(["wmctrl", "-lG"]).decode("utf-8").splitlines()]
        xpos = max([math.ceil((int(w[2])+span[1][0])/res[0]) for w in w_list])
        min_x = max(xpos, curr_col+1, hsize)
        if xpos >= hsize:
            set_workspaces(min_x, "hsize")
            set_workspaces(min_x, "hsize")
        ypos = max([math.ceil((int(w[3])+span[1][1])/res[1]) for w in w_list])
        min_y = max(ypos, curr_row+1, vsize)
        if ypos >= vsize:
            set_workspaces(min_y, "vsize")
            set_workspaces(min_y, "vsize")
    except subprocess.CalledProcessError:

res = get_res()
t = 0

while True:
    span = wspace()
    if span != None:
        cols = int(span[0][0]/res[0]); rows = int(span[0][1]/res[1])
        currcol = int((span[1][0]+res[0])/res[0])
        if all([currcol == cols, cols*rows < max_ws]):
            set_workspaces(cols+1, "hsize")
        currrow = int((span[1][1]+res[1])/res[1])
        if all([currrow == rows, cols*rows < max_ws]):
            set_workspaces(rows+1, "vsize")
        if t == 10:
            clean_up(currcol, currrow)
            t = 0
            t = t+1

How to use

  1. Copy the script below into an empty file, save it as add_space.py
  2. In the head section of the script, edit the lines if you like other settings (maximum number of workspaces, default matrix e.g. 2x2):

    # --- set default workspaces below (horizontally, vertically)
    hsize = 2
    vsize = 2
    # --- set the maximum number of workspaces below
    max_ws = 10
  3. Test- run it by the command:

    python3 /path/to/add_space.py
  4. If all works fine, add it to your startup applications: Dash > Startup Applications > Add the command:

    /bin/bash -c "sleep 15 &&  python3 /path/to/add_space.py`


As always, the script is extremely "low on juice", and does not add any noticeable load to your processor.


The story below is a bit complicated and mostly an explanation on the concept and the procedure, rather than the coding. Only read if you're interested.

How to calculate the needed workspaces (example columns)

The output of wmctrl -d looks like:

0  * DG: 3360x2100  VP: 1680,1050  WA: 65,24 1615x1026  N/A

In the output, VP: 1680,1050 gives us information on where we are on the spanning workspace (the matrix of all viewports). This information is only useful if we also have the screen's resolution, since e.g. 1680 could be the width of two (unlikely, but still) or one time the screen.
Luckily, we can parse out the screen's resolution from the command xrandr.

Then if we know the screen's x-size is 1680 and we currently are on VP: 1680,1050, we know we are on the second column in the workspace's matrix. Since we also know the size of the total matrix (DG: 3360x2100, also from the output of wmctrl -d), we know the current matrix includes two columns (3360/1680), and we are on the "last" one.

enter image description here

The script will then send an instruction to add a column to the matrix by the command:

dconf write /org/compiz/profiles/unity/plugins/core/hsize <current_viewport_column+1>

This is the principle.

How to calculate the workspaces to remove (example columns)

Once per 10 seconds, the script runs the command to list all currently opened windows, with the command:

wmctrl -lG

This gives us information on the window's position also, looking like:

0x04604837  0 3425 24   1615 1026 jacob-System-Product-Name Niet-opgeslagen document 2 - gedit

In the output, 3425 is the x-position of the window. This figure is however relative to the current workspace (left side). To know the absolute position of the window (x-wise) in the workspace-matrix, we have to add the first number of the current viewport information (e.g. VP: 1680,1050, from the output of wmctrl -d).
Let's however, for simplicity reasons, assume we are on viewport 1,1 (topleft viewport), so the window's relative position equals its absolute position.

Since the screen's resolution is 1680, we know the window is on column 3425/1680, rounded up, since everything between 3360 and 5040 is on the same column in the matrix (between 3 and 4 times the resolution). For proper calculation we use math.ceil() (python)

Since the script also practices the rule to always have an extra workspace on the right/below, we need to set the number of columns to the highest value of:

  • the current workspace column + 1
  • the last column with a window on it
  • the default number of columns, as set in the head of the script

And so the script does :)

The rows are managed in exactly the same procedure.