Start programs like a pro with XBindKeys


Author: Joe 'Zonker' Brockmeier

Desktop environments like KDE and GNOME make it possible for you to assign keyboard shortcuts to a number of global operations, but don’t make it easy to create shortcuts to run any program you’d like. To set up shortcuts for programs, XBindKeys is the way to go.

XBindKeys, as the name implies, binds keys to a command. It runs in the background and waits for you to run a shortcut that’s defined in its configuration file. It handles virtually any key press (not just the modifier keys that your window manager recognizes) and is easy to set up.

Getting started with XBindKeys

I’m running XBindKeys on Ubuntu and Debian, which provide XBindKeys packages. Just run apt-get install xbindkeys xbindkeys-config as root, or using sudo, and you should be off to the races. Fedora Core 6 users (and possibly earlier versions) can run yum install xbindkeys to install XBindKeys — though Fedora doesn’t include the XBindKeys Config utility we’ll talk about in a moment. If you’re running Solaris, see the XBindKeys site for source and packages.

XBindKeys Config – click to view

After installing XBindKeys, you’ll need to generate a configuration file. The easiest way to do this is by running xbindkeys --defaults > $HOME/.xbindkeysrc, or just copying one of the example configuration files from /usr/share/doc/xbindkeys/examples to .xbindkeysrc under your home directory, if you’re on a Debian or Ubuntu system.

With XBindKeys you have two ways to customize your configuration — by hand-tweaking the configuration with a text editor, or by using the XBindKeys Config GUI utility. Let’s take a look at the configuration format, and then we’ll cover setting up commands using the GUI utility.

The basic syntax is very simple:

#Comment about the entry
Modifier + key

The first line is pretty obvious — that’s a comment that explains what the entry is. The comment is optional, but recommended. The second line includes the command you’d like to run when you use the shortcut. Finally, the third line provides the shortcut itself.

One of the nice things about XbindKeys, versus setting shortcuts in KDE or GNOME, is that you can also use mouse buttons in a shortcut. For instance, if you wanted to use Control plus the third mouse button to launch a program, you could use this entry:

#Mouse plus control launches gaim
Control + b:3 + Release

XBindKeys also accepts raw keycodes. If you wanted to use Ctrl-Shift-m to open an xterm, you could add it to the config file like this:

#Using keycodes
m:0x5 + c:58

The first part of the entry, m:0x5 is the modifier key — in this case Ctrl and Shift pressed together. If you just press the Shift key, it would be m:0x1, and the Control key by itself would be m:0x4. The second part, c:58, is m by itself.

What if you don’t know the keycode for a key or key combination? No problem. Run xbindkeys -k from a terminal (you’ll need to see its output), and it will display a small dialog box. Make sure that box has focus, and press the key or keys that you want to get the output for. Let’s say you press Ctrl-Shift-h; xbindkeys will provide output that’s ready to cut and paste into your .xbindkeysrc, like this:

Press combination of keys or/and click under the window.
You can use one of the two lines after "NoCommand"
in $HOME/.xbindkeysrc to bind a key.
    m:0x15 + c:43
    Control+Shift+Mod2 + h

Now all you have to do is provide the command to be run in place of "NoCommand", reload xbindkeys, and you’re ready to go.

If you prefer to go the GUI route, you can use the XBindKeys Config utility. Run xbindkeys-config and you’ll see a window similar to Figure 1. Make sure that you’ve created a configuration file first or the utility will crash when you try to save your settings.

On the left side, you’ll see all of the existing shortcuts. On the right side you can see the shortcut editor. At the bottom is the toolbar for working with existing shortcuts. Let’s create a new shortcut by pressing New.

Now you’ll see a new entry in the shortcuts pane, and the cursor will be placed in the Name field on the right side. Give the new shortcut a name, then press the Get Key button to assign a shortcut to the action. After you’ve assigned a shortcut, add the command you want to run to the Action field. Once you’re happy with everything, click the Apply key and try out your shortcut. XBindKeys should run your command when you press the shortcut.

One thing to watch out for — if you’ve assigned a shortcut that’s already in use, you may not get the results you expect. I’ve tried assigning a number of shortcuts that conflict with global shortcuts that are already assigned under KDE. If that happens, you’ll either need to change the shortcut, or reconfigure KDE’s shortcuts.

After you’ve set up your shortcuts you can click the Save & Apply & Exit button and start using them. However, you need to restart xbindkeys anytime you modify its configuration file — it won’t re-read the configuration automatically. I usually just use killall -HUP xbindkeys.

Also, xbindkeys needs to be running in the background to work, so you will want to make sure it runs when X starts. The best way to do this, as long as you’re logging in via KDM or GDM, is to put xbindkeys in your ~/.bashrc file.

With a little work up front, XBindKeys can make your life a lot easier. Give it a try and you’ll be hooked in no time.