A Beginner’s Guide to Using xbindkeys for Custom Key Bindings in Linux

Siva
3 min readMar 28, 2023

--

Introduction

xbindkeys is a program that allows you to bind keys or mouse buttons to commands in Linux. It is a simple and efficient tool that can be used to automate repetitive tasks, improve workflow, and increase productivity. In this article, we will learn about xbindkeys, And It’s installation, and how to use it.

Installation

Before we dive into the usage of xbindkeys, we need to install it. xbindkeys is available in most Linux distributions, and you can install it using your system’s package manager. For example, on Ubuntu, you can install it using the following command:

sudo apt-get install xbindkeys

Usage

Once you have installed xbindkeys, you can start using it to bind keys or mouse buttons to commands. To do this, you need to create a configuration file called .xbindkeysrc in your home directory. This file contains the bindings between keys and commands.

Syntax

The syntax for the .xbindkeysrc file is straightforward. Each binding consists of three parts:

"<command>"
<key>

The <key> represents the key or mouse button that you want to bind, and the <command> represents the command that you want to execute when the key or mouse button is pressed.

Example

Let’s create a simple binding to open the terminal when we press the F12 key. To do this, we need to create a .xbindkeysrc file in our home directory and add the following line to it:

head -20 ~/.xbindkeysrc
"gnome-terminal"
shift+F12

Save the file and run the following command to reload the configuration file:

xbindkeys -p

Now, when you press the Shift + F12 key, the terminal will open.

Advanced Usage

You can also use xbindkeys to execute complex commands or scripts. For example, let’s say we want to create a binding to change the desktop wallpaper. We can do this by creating a script called change_wallpaper.sh with the following content:

#!/bin/bash
feh --randomize --bg-scale /path/to/wallpapers/*

Make sure you make the script executable using the following command:

chmod +x change_wallpaper.sh

Now, we can create a binding to execute this script by adding the following line to the .xbindkeysrc file:

"/path/to/change_wallpaper.sh"
shift+F12

Exo-open is a command-line tool that serves as a frontend to the Xfce Preferred Applications framework. If you’re leveraging this framework, the following step can be used to open the terminal emulator.

"exo-open --launch TerminalEmulator"
shift + F12
tail -4 ~/.xbindkeysrc

Save the file and reload the configuration using the xbindkeys -p command.

xbindkeys -p

Conclusion

xbindkeys is a simple and powerful tool that can help you automate repetitive tasks and improve your workflow. By binding keys or mouse buttons to commands, you can execute complex tasks with a single keystroke. We hope this article has helped you understand the basics of xbindkeys and how to use it.

--

--