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?


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.

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
if [ $target = $total ]; then
wmctrl -s $target
  • Indeed. I was actually trying to use the name of each workspace (D1 and D2) instead of their numbers (0 and 1). Should have read further down the man page. =) – Malabarba Oct 5 '10 at 21:01
  • Nice script - it works for any number of workspaces. Good stuff! ...how to hook that script up to a keystroke? – pbr Oct 5 '10 at 23:57
  • 3
    Save it anywhere as a text file, and mark it as executable (chmod +x filename). Go to "System -> Preferences -> Keyboard Shortcuts". In this menu, create a new shortcut, give it whichever name you like and point it to your saved file. For instance, if you save the file as "script.sh" in your home folder, what you have to write in the "Command" input field is: "/home/yourusername/script.sh". – Malabarba Oct 6 '10 at 5:40
  • Awesome. Thanks, Bruce, for documenting the remaining steps on this. – pbr Oct 14 '10 at 7:55
  • p.s. I stick with putting executables in ~/bin so I'd shortcut to that location as well. – pbr Oct 14 '10 at 7:56

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}'`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.