Use mouse buttons to switch workspaces in Xubuntu/XFCE

2019 May 11

TL;DR

  1. Configure multiple workspaces in XFCE.
  2. sudo apt install xbindkeys xbindkeys-config xdotool xev
  3. xbindkeys --defaults > ~/.xbindkeysrc
  4. Determine the numbers of the mouse buttons with xev -event button
  5. Try out xdotool to switch workspaces from the command line:
    xdotool set_desktop $(expr $(xdotool get_desktop) - 1) (left) and
    xdotool set_desktop $(expr $(xdotool get_desktop) + 1) (right)
  6. Add config to ~/.xbindkeysrc:
    
        # Mouse button 9 (or whatever number xev told you)
        "xdotool set_desktop $(expr $(xdotool get_desktop) - 1)"
            b:9
        # Mouse button 8 (or whatever number xev told you)
        "xdotool set_desktop $(expr $(xdotool get_desktop) + 1)"
            b:8 
                        
  7. Run xbindkeys
  8. If it didn't work, try xbindkeys-config and click Apply.

Using workspaces is very efficient because it helps you group applications based on what they do. On macs, Apple has introduced this concept of full screen applications, and I find it really useful. Especially since I can use a trackpad gesture to swipe between them.

What about Linux?

I use Xubuntu, I find it very fast and it stays out of the way. Plus, as a personal preferance, I really don't like the Ubuntu interface. So for me Xubuntu is bringing all the power and ease of use of Ubuntu, but with a toned down GUI — XFCE.

XFCE is very simple, nice and customizable. Like other window managers, it has support for multiple workspaces (or desktops, as they are called in Windows), which is really great for my productivity.

Switching between workspaces is not that easy as on my MacBook, though. It's either clicking the pager (that can be added to the bar) or using key combinations. I really want to do it with a click of a mouse button.

Switching workspaces with the mouse

Most mice these days have extra buttons. Mine has the typical left and right buttons and the middle scrolling wheel, but it also has 2 extra buttons on the side. They are configured to be the back and forward buttons by default, but I don't find that very useful.

What I really want is to use these 2 extra buttons to move workspaces left and right.

Configuring mouse buttons to switch workspaces

I think there are many ways to achieve this, but I'm going to describe the solution that worked for me in the hope that it may be usefull for other people as well.

This solution combines xbindkeys with xdotool to achive the desired outcome.

With xbindkeys, you can bind different keyboard and mouse events to commands.

With xdotool, you can turn workspace actions into commands to feed into xbindkeys.

We will also use xev to determine the numbers of the mouse buttons, in case you don't already know them (or you just don't feel like guessing).

Install everything:


    sudo apt install xbindkeys xbindkeys-config xdotool xev
        

Try out xdotool to see if you can make it switch workspaces. For example this should switch workspaces to the right:


    "xdotool set_desktop $(expr $(xdotool get_desktop) + 1)"
        b:8 
        

Replace + 1 with - 1 and it should switch workspaces to the left.

Initialise xbindkeys config file by running


    xbindkeys --defaults > ~/.xbindkeysrc
        

Add your binding to this file


    # Mouse button 9 (or whatever number xev told you)
    "xdotool set_desktop $(expr $(xdotool get_desktop) - 1)"
        b:9
    # Mouse button 8 (or whatever number xev told you)
    "xdotool set_desktop $(expr $(xdotool get_desktop) + 1)"
        b:8 
        

If you aren't using mouse buttons 8 and 9 or you're not sure, run xev to see which button numbers are good for you. Xev opens up a window in which you can click the desired mouse buttons and details about them will be logged in the terminal.


    xev -event button
        

To apply your config, you can just run xbindkeys and you're done.

If that didn't work or if you need to make changes, I found that running xbindkeys-config and they clicking Apply helps. It's also useful to see what bindings you have there and maybe remove those you don't need.

Happy workspace switching!