Using XBindKeys on Ubuntu Linux to Remap Key Commands

Once again I have started to use Ubuntu GNU/Linux to try to get away from Windows and explore more freedom respecting software. Of course there are going to be a lot of utilities you use in Windows that are not available in Linux so you have to find the equivalents. One of my must have utilities on Windows is AutoHotKey, especially since I use a TypeMatrix compact keyboard with a Dvorak layout. For example, my Z key is on the right side of the keyboard so I like to remap Ctrl + ; (semicolon) to activate Ctrl + z to undo things with my left hand.

The idea is that you use xbindkeys to listen for keyboard commands (input) and then you use xvkbd to type your desired keys (output).

Here is how to remap a combination of keys to another combination of keys on linux:

Install xbindkeys

sudo apt-get install xbindkeys

Create the default config file for xbindkeys

xbindkeys --defaults > /home/your-user-name/.xbindkeysrc

When thats done, install xbindkeys-config, the GUI for xbindkeys (note: the GUI is optional, you could just edit the config file with a text editor).

sudo apt-get install xbindkeys-config

Now the utility the actually does the “typing”

sudo apt-get install xvkbd

Once each is installed, start xbindkeys by bringing up “Run Application” with ALT -F2.

xbindkeys

If you want to launch the GUI editor you can run xbindkeys-config, but I found it to be more confusing than the text file.

Configuring your Shortcuts and Macros

Open your configuration file in your favorite text editor. It is called .xbindkeysrc in your home directory.  Use the # character to comment out lines, such as the default macros placed by the program’s author — after I installed it, Ctrl + F was launching an xterm window all the time.

At the end of the file, put this:

"xvkbd -xsendevent -text "Cz""
   control + semicolon
The second line is the keystroke to invoke the operation — kind of counterintuitive but that is how it is. Here it is listening for Control + semicolon.  To find the syntax for the key being pressed, you can run the xbindkeys-config utility and click the Get Key button. Press any key combination and it will print out what you did. From there you can just copy or edit the text of the command.
The first line is the command line operation to send when your desired key combination occurs. Here I’m running xvkbd with some flags. -xsendevent tells it to send an XEvent to whatever the active window is (and active input field). -text means type the block of text in quotes. C (backslash C) means hold Ctrl while pressing the next key, which is the letter “z”.   For more xvkbd syntax, look at http://man-wiki.net/index.php/1x:xvkbd or this chart:
– r – Return
– t – Tab
– b – Backspace
– e – Escape
– d – Delete
– S – Shift (modify the next character)
– C – Control (modify the next character)
– A – Alt (modify the next character)
– M – Meta (modify the next character)
– [keysym] – the keysym keysym
Please note that modify with “S will be ignored in many cases. For example, “aCbScDCE will be
interpreted as a, Control-b, c, Shift-D, and Control-Shift-E.
Instead of using xvkbd to send text strings, you could probably use the xmacro utility to create more advanced macros to playback. You can also run any program you want, just put the command within the double quotes.
Example: Launch Firefox with Ctrl + f
"firefox"
   control + f

Now I just have to find an easy way to reload my xbindkeysrc config file when I make changes. Right now I’m killing the process and launching it again.

Final note: you can set xbindkeys to launch on startup — in ubuntu, just go to System -> Preferences -> Startup Applications and add a new command xbindkeys.