Ubuntu – How to bind a key to cycle between workspaces

10.04shortcut-keysworkspaces

I'm using 2 workspaces right now, and I'd like to be able to cycle through them using a single shortcut. At the moment, I can do Ctrl+Alt+Right to switch to workspace 2, and Left switches to 1, but I'd like a single key (or key combination) to switch to the workspace I'm not in right now. That way I would only need that shortcut, and not need two.

I'm running Ubuntu on an old machine, and that keeps me from enabling compiz at all. I tried writing a script using both wmctrl and xdotool, but neither work properly (no matter what I do, they both will only switch to workspace 1, and never to 2).

Any suggestions?

Best Answer

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