[TUTORIAL] bind Hyper_L key to Super_L key - revised- 9/15/19

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
Mister Cabbage

[TUTORIAL] bind Hyper_L key to Super_L key - revised- 9/15/19

Post by Mister Cabbage »

tutorial - bind hyper-l to super-l key

* updates

** 9/15/19

revised my tutorial to make it a bit more organized and to fix a typo i discovered after following my own tutorial after reinstalling mint.

* introduction

hello everyone. i don't post much on the forums here, but i wanted to share a neat trick with with emacs. if you have a keyboard with dual super keys [also referred to as the 'windows key'] it's possible to re-purpose one into a new mod key for use inside emacs. it's also handy for key combos outside of emacs as well, so even if you don't use that text editor this tutorial may still be of interest to you. this tutorial is written with the intention of re-purposing your left super key into a left hyper key.

* background

what is a hyper key?

it's a mod key, similar to CTRL*SHIFT*ALT. it dates back to the era of dedicated lisp machines, the type of computer where emacs was first developed. from my own research, it would appear the hyper key was typically reserved for user key combos. it made it's debut on the space-cadet keyboard which you can read more about on this linked article:

https://en.wikipedia.org/wiki/Space-cadet_keyboard

here's a direct link to an image of the space-cadet keyboard:

Image

emacs doesn't bind anything to the hyper key by default. this makes it an excellent choice in binding the hyper key to an extra physical key on your keyboard. furthermore, it gives you an extra mod key for combos inside of linux mint itself. i have only personally done this in mint, but i assume it should work in any linux version. feel free to correct me if i'm wrong.

* walk-through

quick note: if at anytime you've somehow messed up the keyboard layout and bindings, you enter the following in terminal:

Code: Select all

setxkbmap -layout us
just remember to replace the 'us' part with whatever is appropriate for your region such as 'fr' for french, and so on. also if you're doing this after creating any files/startup entries i've instructed you to, you'll need to delete those files/entries after issuing the command above

** check existing bindings

before you do anything, open a terminal window and type the command below:

Code: Select all

xmodmap
this command will show you the current line-up of bounded mod keys, their key-codes, and which mod group they are assigned to. i believe the key-codes shown on this output are in hex format.

specifically what i want you to look for is a group called mod3. it should be blank. this is important as this is the mod group where the titular hyper key will be assigned after you follow the instructions for this tutorial.

in mod4 you should see keybindings for super-l, super-r, and hyper-l keys.

i'd assume hyper-l is already bound to some key-code in the off chance that a user ever connects a keyboard with an actual hyper key, it will already be assigned and detected by the operating system automatically. it more or less exists like a phantom limb for most modern keyboards, if that makes sense to you.

** find existing key-code for super-l

in order to reassign your super-l key, you must find out its corresponding key-code.

inside a terminal window, you need to type the command below:

Code: Select all

xev
this command will cause the terminal window to begin displaying info about keyboard. it also summons a pop-up window that is used for recording and displaying mouse input. ignore the popup and press super-l several times.

what you're looking for is any text referring to Key Press event or KeyRelease event

once you locate that bit of text, locate a line which should start out something like state 0x10, keycode

read the numbers immediately after the word keycode in that line. in my case, this keycode number was 133, depending on your keyboard/layout it may or may not be different.

** config file

inside the terminal do the following:

Code: Select all

touch ~/.Xmodmap
after that, enter the command below:

Code: Select all

nano ~/.Xmodmap
side-notes [skippable]:

touch: if you aren't already aware, touch is a shell command to create a file, followed by the location of where to place it along with the name you've chosen for it.

~/: this refers to your home folder

nano: this opens a cli text editor called nano. usually in guides, people use gedit, or in the case of linux mint and cinnamon, xed. i like nano for terminal work and scripting in general. it's installed on mint by default. when you enter this command with the name/location of the file you just created, it will then be opened inside nano within the terminal window.

*** file contents

once inside the .Xmodmap file, you need to place the following text:

Code: Select all

clear mod3
clear mod4
keycode # = Hyper_L
add mod3 = Hyper_L
keycode # = Super_R
add mod4 = Super_R
notice how it's set to clear mod4? this is vital to get the binding to work successfully. you're clearing out mod3, just to be safe, but you must also clear out mod4 in order to free up the hyper-l key for re-purposing. it won't work otherwise. you'll then have to re-add super-r back to mod4 in order to ensure that key behaves normally.

where the # symbol appears above, you need to replace with the appropriate key-code.

in my case, my .Xmodmap file looks like so:

Code: Select all

clear mod3
clear mod4
keycode 133 = Hyper_L
add mod3 = Hyper_L
keycode 134 = Super_R
add mod4 = Super_R
if you're editing this file inside nano as i instructed, then press CTRL+S to save the file, then press CTRL+X to close nano.

*** source the config file

enter the following command inside the terminal:

Code: Select all

xmodmap ~/.Xmodmap
basically this makes the .Xmodmap file load and the keybinding will be effective immediately. it won't survive a reboot, and if i remember right, it you'll need to apply it after logging out/in. there's a way to fix this, and i'll get to it in a moment.

for now, test the results. rerun the xev command inside a terminal window. again, ignore the pop-up window as we are not concerned with mouse-related events. press the super-l key. all previous references to super-l should be gone, and now you should see hyper-l popping up and being associated with super-l's key-code.

additionally, if you are an emacs user, you could also test the key bind inside there. simply open emacs and press hyper-l [set to the physical super-l key] and another key such as i.

emacs should say H-i is undefined.

references to the hyper key inside emacs are stylized as a capitol 'H' same as ctrl is represented as 'C' and the alt/meta key as 'M'.

if you saw H-i is undefined that's good! emacs is already capable of accepting the hyper key for key combos and this is just to show you it is trying to interpret a command from it. you'll have to actually bind something inside of emacs itself if you want H-i to mean something there however.

** reboot and suspend

if you've followed my instructions so far, you'll note that it becomes necessary to run:

Code: Select all

xmodmap ~/.Xmodmap
every time that you reboot your computer, or wake it back up after suspending. if you don't, the keyboard will revert to the default layout. this is not a problem and i will show you how to fix this.

you need to make a startup entry to run the command from above automatically every-time that you log into mint.

*** startup entry

press the super-r key to open the start menu and type start

you should immediately see an entry for startup applications. you need to hit the enter key to open the sub-menu for it.

on the startup entry sub-menu, look at the bottom of the window for a + symbol. click on that, then click on custom command.

you will see a window popup that says add startup program along with fields for name, command, comment, and startup delay.

for name, keep it simple such as:

hyper-l

for command put the following:

Code: Select all

xmodmap ~/.Xmodmap
for comment add something like:

binds hyper-l to super-l key

for startup delay:

do nothing for this line, just leave it blank.

with everything filled out, click the add button, and the entry will be added to the list of startup applications and commands.

*** suspend state

you will need to create a service file which in turn runs a script to reload the .Xmodmap file after coming back from a suspend state. this is necessary, otherwise, going to and returning from suspend will restore the default keyboard layout.

**** create the service file

inside the terminal do this:

Code: Select all

sudo touch /etc/systemd/hyper-l.service
now that the empty document exists, we need to edit it so do:

Code: Select all

sudo nano /etc/systemd/hyper-l.service
and paste in the following text:

Code: Select all

[Unit]
Description=reset super-l to hyper-l
Before=sleep.target
StopWhenUnneeded=yes

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStop=/path/to/your/script/hyper-l-fix.sh

[Install]
WantedBy=sleep.target
if you are doing this inside nano per my instructions, press CTRL+S to save and CTRL+X to close the file and exit nano.

**** script for service file

you need to create the script that will be run after waking from suspend. if you don't create the script, the keybinding won't be applied after returning from suspend, and the default keyboard layout will be restored.

***** location for script

personally i put all my personal scripts inside the subdir 'scripts' inside the 'documents' dir which itself exists inside my home directory.

if you want to do this like me, then first create the subdir as specified below, along with making a blank script file inside there. do this [inside the terminal]:

Code: Select all

mkdir ~/Documents/scripts
then export the path for your scripts inside your .bashrc file:

Code: Select all

nano ~/.bashrc
and enter the following inside the .bashrc file [at the bottom/end of the file]:

Code: Select all

# custom user settings
export PATH="$HOME/documents/scripts:$PATH"
the # symbol here represents a commented line, anything typed after the # symbol won't be interpreted as a command. you could write anything, i just chose 'custom user settings' for my own personal reference.

again if you're doing this inside nano, press CTRL+S to save and CTRL+X to close the file and the text editor.

you will need to load/source the .bashrc file to make the added path for your scripts effective immediately.

to source the .bashrc file, type the following inside the terminal and press enter:

Code: Select all

source ~/.bashrc
***** make the script file

do this:

Code: Select all

touch ~/Documents/scripts/hyper-l-fix.sh
then do:

Code: Select all

nano ~/Documents/scripts/hyper-l-fix.sh
now you've opened the blank script file inside nano and you need to fill it out as follows:

Code: Select all

#!/bin/bash

xmodmap ~/.Xmodmap
after you're done, press CTRL+S to save, and CTRL+X to close the file along with nano.

you need to make the script executable:

via terminal:

Code: Select all

chmod +x hyper-l-fix.sh
note: if you correctly sourced the 'scripts' subdir inside your .bashrc file, you should only need to type the exact name of the script file to make it executable. otherwise you would have to:

Code: Select all

chmod +x ~/Documents/scripts/hyper-l-fix.sh
** double-checking and conclusion

and that's it, your done. you can test what you did in this tutorial by rebooting and logging into cinnamon. try pressing the super-l key. if it's doing nothing, that means the startup service correctly ran the .Xmodmap file at startup. also, if it does nothing, that's because you haven't set the hyper-l key to any command inside of cinnamon.

now try suspending your computer, then bring it back. try the key again. if it's still doing nothing, then that's good because the script ran successfully and the super-l key is still bound to hyper-l after suspending. you can always double-check by opening terminal and typing xmodmap to see what's bound.

now you have a new mod key to use inside of emacs, or anywhere in linux.
Post Reply

Return to “Tutorials”