Change Keyboad Bindings (Shortcuts) In the Virtual Console
In this article, I will discuss how to adjust the keyboard bindings of the Linux virtual console in order to trigger new events.
Join the DZone community and get the full member experience.
Join For FreeThe virtual console, also known as the terminal or command line interface, is a powerful tool in Linux for performing various tasks and executing commands. One aspect of customization that can greatly enhance your productivity is modifying the keyboard bindings in the virtual console. This article will guide you through the process of changing keyboard bindings to suit your preferences and streamline your workflow.
Before diving into customizing keyboard bindings, it's important to familiarize yourself with the virtual console. The virtual console provides a text-based interface for interacting with the operating system. It allows you to execute commands, manage files, and perform system configurations without the need for a graphical user interface.
Based on my own work on a client's project and different comments from online communities such as StackOverflow, I will explain the tools and concepts by using an example in order to change the keyboard bindings of a virtual console.
Let's define the point where we come from, the starting point of the example. It's a kind of common sense if you want to scroll down or up within the virtual console (aka tty). You use the page up or page down buttons.
Most of the Linux distributions have already installed the correct packages to provide this feature. In the case this feature is disabled or not included on your machine, let me show you what you need to do to enable it. Let's assume we use a Debian-based machine as Ubuntu. One possible way to activate this feature is by installing the GPM package on your machine. GPM stands for General Purpose Mouse and provides a cut-and-paste utility and mouse server for the virtual console. Right after the installation, you should be able to scroll up and down by the page up and down buttons on your keyboard.
Let's carry on and define the goal of this article. According to the subject of this article and the start point of the example, after the change of the keyboard bindings of the virtual console, we will be able to scroll up and down with the arrow buttons instead the page up and down buttons on your keyboard.
Implement the Changes
Let's define the tools to manipulate the keyboard layout on your machine. Basically, you need the following tools: loadkeys
, dumpkeys
and showkey
. It's a good manner before you continue with this article, read their manpages and inform yourself about their options. Be aware that you can use these tools only in the virtual console.
The keymap file contains the predefined mappings between keys and their corresponding characters or functions. To customize the keyboard bindings, you need to locate the keymap file associated with your virtual console and change the entries.
How do we use these tools in order to change key binding? Let's define a short guide to reach the goal of the article:
Create a Backup of the Current Mapping
dumpkeys > backup.kmap
In case of something goes wrong, we can restore the keymap by using the following command:
$ loadkeys backup.kmap
Determine the key codes that are assigned to your keys and the key code names:
$ showkey
By pressing the key on your board, you will see the key codes in your virtual console. The key codes of the arrow up and arrow down buttons are 108 (arrow up) and 103 (arrow down). To scroll up and down, the default is the key combination of shift and page up or shift and page down. The key code of the page-up button is 109, and the key code of the page-down button is 104. By using the command, dumpkeys
we can define the starting point of our example as follows:
shift keycode 109 = Scroll_Forward shift keycode 104 = Scroll_Backward
There is one last thing you need to do to finish this step. Execute the following command.
$ dumpkeys | head -1
The first line of the keymap line from dumpkeys
is:
keymaps 0-127
Create a Keymap File That Define the New Bindings
In this step, we create a new keymap file newBindings.kmap
by combing all information from the previous step. There are no options defined for the combination of shifts 108 and 103. For that reason, the keymap file should like
keymaps 0-127
shift keycode 108 = Scroll_Forwardshift keycode 103 = Scroll_Backward
Load the New Keymap File
$ loadkeys newBindings.kmap
With loadkeys
we extend the current keymap schema of the virtual console. Instead of using a text file, we also can use
$ loadkeys <<EOF
shift keycode 108 = Scroll_Forward
shift keycode 103 = Scroll_Backward
EOF
Make Changes Permanent
In the first place, check if the file /etc/rc.local
does exist. In case of the file does exist, add the following line at the very end:
$ echo "/usr/bin/loadkeys /[...]/newBindings.kmap
" >> /etc/rc.local
After a reboot of the machine, the new key bindings should take effect immediately. Test the modified key bindings in the virtual console to ensure that they work as expected.
In the case of the file /etc/rc.local
does not exist, we can create the file with a text editor:
$ nano /etc/rc.local
Paste the following lines:
#!/bin/sh -e
COMMANDS
exit 0
And replace "COMMANDS" with the commands to be executed at the system startup:
/usr/bin/loadkeys /[...]/newBindings.kmap
Replace [...] with the path to the file.
Don't forget to add the execute permission on the file rc.local
by:
$ chmod +x /etc/rc.local
And make sure the proper service is enabled. You can check it by:
$ systemctl enable rc-local.service
In case of /etc/rc.local
does not exist, there is another way in order to implement persistent changes by creating a cron job that will be executed at every start of the machine.
First, create a bash script with the name load_new_bindings.sh
:
#!/bin/sh -e /usr/bin/loadkeys /[...]/newBindings.kmap exit 0
Next, open the crontab with:
$ crontab -e
And add the line
@reboot /[...]/load_new_bindings.sh
Replace [...] by the path to the file.
Conclusion
Customizing the keyboard bindings in the virtual console of a Linux machine allows you to optimize your workflow and improve efficiency. By understanding the keymap file structure and following the steps outlined in this article, you can easily modify key bindings to match your preferences. Experiment with different configurations, but remember to maintain a backup of the default keymap file to revert changes if needed. With a personalized keymap, you'll have a more tailored and comfortable experience when using the virtual console.
Opinions expressed by DZone contributors are their own.
Comments