Monday, May 11, 2009

Rotate desktops/workspaces in Gnome

I installed Ubuntu 9.04 on my work desktop computer with Gnome. It took me a while to figure out how to get more than 2 desktops to show:

1. Right click on the panel.
2. Choose "Add to panel"
3. Find "Workspace Switcher"
4. Click the "Add" button
5. In the panel, right click on the Workspace Switcher
6. Click "Preferences"
7. Adjust the number of workspaces

Apparently, multiple desktops are called "workspaces" in Gnome, where they are called "desktops" in KDE. Good to know.

After setting the number of desktops to 4, I removed the workspace switcher from the panel. I use keyboard shortcuts to switch, not point and click with the mouse.

Next, I realized that the keyboard shortcuts don't work as expected. I use Ctrl-Alt-Left and Ctrl-Alt-Right to move between desktops, but then I get to desktop #4, I expect that another Ctrl-Alt-Right will move to desktop #1. Not so. Much googling later, I found these instructions and scripts at

http://reachbeyondgrasp.blogspot.com/2008/01/wrapping-workspaces-in-gnome.html

I'll repeat the gist of that article here and add some additional detail. First, install wmctrl:

sudo apt-get install wmctrl

Then use the scripts. I'm posting the scripts here also for convenience. Just copy these scripts to two files, and make them executable:

------- ws_wrap_right.sh -----
#!/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


----- ws_wrap_left.sh -----
#!/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

----- -----

Next, bind the keyboard shortcuts to these scripts:

1. Start gconf-editor (from a terminal or Alt-F2)
2. Drill down to /apps/metacity/keybinding_commands
3. Right click on command_1, select "Edit key"
4. Enter the full path to ws_wrap_left.sh
5. Click the "OK" button.
6. Right click on command_2, select "Edit key"
7. Enter the fuil path to ws_wrap_right.sh
8. Drill down to /apps/metacity/global_keybindings
9. Find run_command_1, right click, "Edit key", enter <control><alt>Left
10. Find run_command_2, right click, "Edit key", enter <control><alt>Right
11. Find switch_to_workspace_left, right click, "Edit key", clear the value
12. Find switch_to_workspace_right, right click, "Edit key", clear the value

Now workspaces should rotate completely, so when you're on desktop #4 and you press Ctrl-Alt-Right, you'll be on desktop #1.

Why does such a simple thing have to be so hard?