1. Overview

Setting keyboard shortcuts can increase efficiency and productivity by allowing users to perform several tasks without using any input devices. Additionally, it can help in faster navigation, quick launching of applications, and effective window management.

In this tutorial, we’ll explore two methods to set keyboard shortcuts in Linux.

2. Using bind

We can use the bind tool to personalize and enhance the user experience by setting custom keyboard shortcuts from the terminal. It allows us to perform multitasking and utilize the terminal more efficiently.

First, we install the bind tool using the apt command:

$ sudo apt install bind9

Let’s check the installation status of the bind tool using the named command:

$ named -v
BIND 9.18.12-0ubuntu0.22.04.3-Ubuntu (Extended Support Version)

Now, we use the bind tool to set a keyboard shortcut within a terminal. We can define a shortcut for a specific command or text that we use frequently in the terminal:

$ bind '"\C-x":"echo \"Hello, Welcome to the Linux Tutorial\""'

Here, C-x represents the Ctrl+E key. Therefore, whenever we press the Ctrl and E keys together, the assigned text will be displayed in the terminal. Let’s take another example. Now, we want to set a shortcut to open a website:

$ bind '"\C-b":"xdg-open http://www.youtube.com"'

Here, C-b denotes the Ctrl+B key. Additionally, we use the xdg-open command that opens the YouTube website using the default web browser of the system. It’s important to note that the assignment of keyboard shortcuts using this approach is temporary and only works in the current terminal session.

3. Using xbindkeys

xbindkeys is a useful tool in Linux that enables us to customize the desktop environment by setting custom keyboard shortcuts. Additionally, it facilitates advanced configuration, flexibility, and text editing options.

We can install the xbindkeys tool either via the package manager or from the terminal:

$ sudo apt-get install xbindkeys

Let’s check if we installed the tool correctly:

$ xbindkeys --version
xbindkeys 1.8.7 by Philippe Brochard

Now, to set custom keyboard shortcuts, we need to create a configuration file for the xbindkeys tool:

$ xbindkeys --defaults > ~/.xbindkeysrc

We created a default configuration file named xbindkeysrc in this case. Furthermore, let’s edit the configuration file and set our first custom keyboard shortcut. To edit the configuration file, we utilize the nano editor:

$ nano ~/.xbindkeysrc
GNU nano 6.2                 /home/sam/.xbindkeysrc                           
# For the benefit of emacs users: -*- shell-script -*-
###########################
# xbindkeys configuration #
###########################
# Version: 1.8.7
# If you edit this file, do not forget to uncomment any lines
# that you change.
# The pound(#) symbol may be used anywhere for comments.
# To specify a key, you can use 'xbindkeys --key' or
# 'xbindkeys --multikey' and put one of the two lines in this file.
# The format of a command line is:
#    "command to start"
#       associated key
# Examples of commands:
"xbindkeys_show" 
  control+shift + q
# set directly keycode (here control + f with my keyboard)
#"xterm"
#  c:41 + m:0x4
# specify a mouse button
#"xterm"
#  control + b:2
#"xterm -geom 50x20+20+20"
#   Shift+Mod2+alt + s
## set directly keycode (here control+alt+mod2 + f with my keyboard)
#"xterm"
#  alt + c:0x29 + m:4 + mod2
## Control+Shift+a  release event starts rxvt
#"rxvt"
#  release+control+shift + a
#
## Control + mouse button 2 release event starts rxvt
#"rxvt"
#  Control + b:2 + Release
##################################
# End of xbindkeys configuration #
##################################

We can modify the configuration file and set our custom keyboard shortcut to perform a particular action. Let’s assign a keyboard shortcut to open the Firefox browser:

"firefox"
 Mod4+Return

Here, Mod4+Return denotes the Windows and Enter keys on our keyboard. Hence, we can directly open the Firefox browser by pressing the Windows and Enter keys together. Similarly, we can set a keyboard shortcut for any application based on the requirement. Finally, we need to save the configuration file and return to the terminal to apply the changes made on the file using the -p option:

$ xbindkeys -p

Now, we successfully set a keyboard shortcut to open the Firefox browser, and we can check it by pressing the Windows and Enter keys together.

4. Conclusion

In this article, we discussed two methods to set keyboard shortcuts in Linux.

The first method is temporary and only works within a terminal session in which keyboard shortcuts are created. On the other hand, using the second method, we can create global keybindings that are not limited to a single terminal session. Additionally, we can use the xbindkeys tool with various Linux desktop environments.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.