i3: Show Desktop Shortcut

Wednesday, 21 Dec 2016

I missed having a button/shortcut to show the desktop, mostly to take a look at Conky’s output.

Thanks to i3’s IPC interface, it is simple to make a script that finds the largest workspace in use. Then we can issue the command to i3 to switch to the succeeding workspace, which will be empty.

#!/usr/bin/env python3
import subprocess
import json

command = "i3-msg"
arguments = ['-t', 'get_workspaces']
output = subprocess.check_output([command] + arguments)

workspaces = json.loads(output.decode('utf-8'))
last_nonempty_workspace = workspaces[-1]['num']
first_empty_workspace = last_nonempty_workspace + 1 


Then, in ~/.i3/config:

# Show desktop (go to first unused workspace)
bindsym $mod+c exec --no-startup-id i3-msg workspace $(~/scripts/i3-empty-workspace.py)

(The --no-startup-id parameter avoids having the cursor displayed as spinning clock.)