Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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
share|improve this answer
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! to hook that script up to a keystroke? – pbr Oct 5 '10 at 23:57
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 "" in your home folder, what you have to write in the "Command" input field is: "/home/yourusername/". – 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}'`
share|improve this answer
This is cool! It's worked for me. Thanks! – Mars Dec 26 '14 at 10:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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