DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • 7 Linux Commands and Tips to Improve Productivity
  • How Do the Docker Client and Docker Servers Work?
  • 9 Useful Interactive CLI Tools for Linux
  • How to Introduce a New API Quickly Using Micronaut

Trending

  • Metrics at a Glance for Production Clusters
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • How to Perform Custom Error Handling With ANTLR
  • How Can Developers Drive Innovation by Combining IoT and AI?
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Change Keyboad Bindings (Shortcuts) In the Virtual Console

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.

By 
Constantin Kwiatkowski user avatar
Constantin Kwiatkowski
·
Jul. 07, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.5K Views

Join the DZone community and get the full member experience.

Join For Free

The 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

Shell
 
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_Forward
shift 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.localby:      

$ 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.

Linux (operating system) Command-line interface

Opinions expressed by DZone contributors are their own.

Related

  • 7 Linux Commands and Tips to Improve Productivity
  • How Do the Docker Client and Docker Servers Work?
  • 9 Useful Interactive CLI Tools for Linux
  • How to Introduce a New API Quickly Using Micronaut

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!