Make your own keybindings in linux using xbindkeys

Are you like me, who want to listen to music while you work? But then your boss comes over and you quickly have to pause it? Or maybe you just want some key bindings on your linux workstation to make life a lot easier, then look no further! xbindkeys is the solution, and I will show you how in this post! it’s really easy to configure and extremely helpful.
In my example, I will use it to pause/unpause my music when i press CTRL+space no matter what desktop I’m on and even if I’m not in the terminal where MOC (the music player I use) is running.

What you need

xbindkeys is the name of the only package you need for this. It should be in the official repository for your distro. Install examples below:

Debian

apt-get install xbindkeys

Arch Linux

pacman -S xbindkeys

CentOS

yum install xbindkeys

Configuration xbindkeys

First create a file in your home directory called “.xbindkeysrc” with the following command:

touch ~/.xbindkeysrc

Then run the command as shows below. In the little window that pops up, press the key you want to see the name for. The window will then close and the terminal will print out what to write in your config to use that key.

xbindkeys -k

It will look something like this
xbindkeys -k
When I press spacebar, the windows closes and the the terminal prints out what I pressed:
xbindkeys pressed space
As you can see, it says in the last line that i pressed “Mod2” and “space”
Mod2 = my num lock
We ignore the numlock, it will show every time if numlock is enabled (might be an other key on your keyboard).
As I want to be able to press CTRL+space I want to know what multiple keys are named. You can run the same command again and press CTRL to see what it’s named, or you can run the program with “-mk” (multiple keys):

xbindkeys -mk

This way you can press multiple keys and view the output in the terminal. To quit press the “q” key or close the window with your mouse.
So, what I found out was that my CTRL key is named “Control” and Spacebar is named “space”
Edit your ~/.xbindkeysrc file using your favorite text editor and add the following lines (replace with the command you want it to run when you press the key combination):

""
    Control + space

now restart xbindkeys:

killall xbindkeys; xbindkeys

If you replace with example “MOCP –toggle-pause” it will pause/unpause your MOC player! just what I needed for when the boss shows up to ask me something.
You can of course get this to run any command you like at any key combination or even a single key/letter, and mousekeys!. Make sure that the key combination you use is not used for something else in your OS.
I tested this in Debian using Openbox, but xbindkeys should work in any environment.
xbindkeys should autostart on its own if you reboot, but if not you can simply add the command “xbindkeys” to rc.local or any other file that will be run on startup, xbindkeys will then start itself in the background and run as a daemon.

Leave a Reply

Your email address will not be published.