How can I bind a key to cycle between workspaces?

Solution 1:

Here's a script that switches to the next workspace, wrapping back to the first after the last. Note that workspaces are numbered from 0, maybe this is what threw you when you tried writing a script.

#!/bin/sh
total=$(wmctrl -d | wc -l)
current=$(wmctrl -d | sed -n 's/^\([0-9]\+\) *\*.*/\1/p')
if [ -z "$total" ] || [ -z "$current" ]; then
  echo 1>&2 "$0: Could not obtain workspace information!"
  exit 2
fi
target=$(($current+1))
if [ $target = $total ]; then
  target=0
fi
wmctrl -s $target

Solution 2:

i know this is old but here are my versions:

For multiple workspaces (as in Gilles's answer):

wmctrl -s `wmctrl -d | awk '$2=="*"{cur=NR} END{print cur % NR}'`

For multiple viewports:

wmctrl -o `wmctrl -d | awk '{gsub(/[^0-9]/," ",$0);x = ($4 + $6 + $8) % $2; if( 
x == 0 ) y = ($5 + $7 + $9) % $3; else y = $5; print x","y}'`