January 26, 2008

Wrapping workspaces in Gnome

One source of frustration sometimes with gnome is that there is no way to wrap workspaces like in KDE, compiz or even the other window managers like XFCE. This means that, if like me you use 'move to workspace right' and 'move to workspace left' frequently while working, it can frustrating not to be able to get back to workspace 1 from the last one in one simple step.

Searching for some way to do this, I came upon this post describing the use of wmctrl to do this. I had never heard of wmctrl before, but it turns out to be pretty cool. So go ahead, install it (sudo aptitude install wmctrl) and now -


raja@eee:~$ wmctrl -d
0 - DG: 800x480 VP: N/A WA: 0,2 800x476 Desk 1
1 - DG: 800x480 VP: N/A WA: 0,2 800x476 Desk 2
2 - DG: 800x480 VP: N/A WA: 0,2 800x476 Desk 3
3 * DG: 800x480 VP: 0,0 WA: 0,2 800x476 Desk 4

raja@eee:~$ wmctrl -s 1 && wmctrl -d
0 - DG: 800x480 VP: N/A WA: 0,2 800x476 Desk 1
1 * DG: 800x480 VP: 0,0 WA: 0,2 800x476 Desk 2
2 - DG: 800x480 VP: N/A WA: 0,2 800x476 Desk 3
3 - DG: 800x480 VP: N/A WA: 0,2 800x476 Desk 4



As we can see, 'wmctrl -d' lists the desktops and places an asterisk to indicate the current desktop. And 'wmctrl -s n' allows us to switch to the desktop n, with the desktops being numbered from 0 onwards.

With this knowledge, it is easy to write a couple of scripts to switch to the desktop on the left and the one on the right, with wrapping enabled.


#!/bin/bash

#ws_wrap_lt.sh

#get number of workspaces
ws=$(wmctrl -d | wc -l)

#current workspace index
cws=$(wmctrl -d | awk '/\*/ {print $1}')

#work space on left
lws=$(($cws-1))

#wrap if required
if [ $lws = -1 ]; then
lws=$(($ws-1))
fi
echo $lws

#change to next workspace
wmctrl -s $lws


and ..


#!/bin/bash

#ws_wrap_rt.sh

#get number of workspaces
ws=$(wmctrl -d | wc -l)

#current workspace index
cws=$(wmctrl -d | awk '/\*/ {print $1}')

#work space on right
rws=$(($cws+1))

#wrap if required
if [ $rws = $ws ]; then
rws=0
fi

#change to next workspace
wmctrl -s $rws


Save the files somewhere and now you can use gconf-editor to map commands to these scripts. Then you can set up a desired key or key combination to run these commands. An option is also to use only one of the scripts and rotate through the workspaces with one key. On the eeepc, I have mapped the 'menu' key to ws_wrap_rt.sh and use just that key to get to any workspace I want. If you use only two workspaces,one key works to toggle the workspaces.

6 comments:

Willem said...

awesome! Just what I needed. thanks!

jared said...

What sucks is that you have to turn off Visual Effects (In Ubuntu it is under System>Preferences>Appearance, Visual Effects tab, select None) for wmctrl to work well. :(

Raja said...

@Jared,

I guess the title is a little misleading. But this is a hack that you would only need in Metacity. If you are using Compiz, wrapping workspaces does not need wmctrl.

EugenMayer said...

When you using Compiz on normal or special mode (Karmic or Intrepid) you will not able to use wmctrl to use switch the workspaces the described way (you will only have one workspace). I have described the way to accomplish that here

http://wiki.impressive-media.de/doc/toggle-or-rotate-through-your-workspace-using-a-shortcut-while-having-compiz-activated-gnome

Raja said...

@EugenMayer
I have mentioned in the very beginning of the post that wrapping workspaces is possible in Compiz without using any other hacks (In Compiz Config Manager, look for the option "Allow Wrap-around). It is only in Metacity that this functionality is unavailable and you need these hacks.

Pablo said...

That is what I was looking for!!!
Thanks!