Making better use of the Caps Lock key in Linux
En Español
How frequently do we really use the Caps Lock key?, This is one of the best positioned keys in our keyboard, as it rests next to the A key, however, in this space we have this Caps Lock key that we seldom utilize. For those who type without looking at the keyboard it is an even more important space, as it is readily available with a minor movement of the left pinky. I am sure that there are people who do utilize it in a daily basis, but it is my understanding that this is a minority.
But this can be easily changed in Linux. And believe me, you wont regret this change once you grow accustomed to it. For a Vim user there is no question about what to do with this key, we can swap it for the Escape key, or turn it into a second Escape key, although I personally didn't want to lose the functionality so I just swapped it. Other possible uses are to turn it into another Backspace key, or into a Control key, we can even give it the functionality of a multimedia key.
KDE and Gnome allows us to change the behavior of the Caps Locks key by adjusting the settings of the keyboard. But for any desktop environment, such as Fluxbox and of course KDE and Gnome themselves, this can be adjusted by defining the new functionality in a text file, and then providing it as a parameter to xmodmap
.
Temas
Change or swap the Caps Lock key functionality in Gnome
Change or swap the Caps Lock key functionality in KDE
Change or swap the Caps Lock key functionality with xmodmap
Running the script when Fluxbox, KDE and X itself start
Return keys to it's original function with xmodmap
Change or swap the Caps Lock key functionality in Gnome
To change the functionality of the Caps Lock key in Gnome, go to System > Preferences > Keyboard. Then, in the Layouts tab, click the Options button, and there you will see a CapsLock key behavior section.
Some useful options are:
- Make CapsLock an additional Backspace
- Make CapsLock an additional ESC
- Swap ESC and CapsLock
Change or swap the Caps Lock key functionality in KDE
To change the functionality of the Caps Lock key in KDE, go to Settings > System Settings, then click on Input Devices, then, with Keyboard selected, click in the Advanced tab, and there you will see the CapsLock key behavior section.
Again, some useful options are:
- Make CapsLock an additional Backspace
- Make CapsLock an additional ESC
- Swap ESC and CapsLock
Change or swap the Caps Lock key functionality with xmodmap
Now, to change this setting in any desktop environment, something that I do for Fluxbox, as it is one of the desktop environments that I utilize, we can make use of xmodmap
. To remap this key using xmodmap
, we need to create a text file called .Xmodmap
(although you can call it anything that you want) and then edit it in our favorite text editor:
touch ~/.Xmodmap
Then we can use either use gedit in gnomegedit ~/.Xmodmap
or kate in KDEkate ~/.Xmodmap
or any other text editor, up to and including Vim.
With xmodmap
we can redefine the functionality of a key by using the expression keysym
, it will translate the machine code to perform a certain keycode
expression, or in simpler terms, it will make a key behave like another.
The keycode
expression is how we address said key functionality, e.g., Caps_Lock
, Control_L
for the left control key, Meta_L
for the left Meta key, Escape
, BackSpace
, or the multimedia keys such as XF86AudioMute
for mute the sound. There is a database of all this keycode expressions in a file called XKeysymDB
, this file is usually located in /usr/share/X11/XKeysymDB
. Additionally, some keys are considered modifiers, as the control key, the shift key, and of course, the caps lock key. This is because this keys are always used in combination with other keys and does not have a specific functionality themselves. We can remove and add modifier keys by using remove Shift/Lock/Control
and add Shift/Lock/Control
. In this file, lines starting with ! are considered commentaries. This explanation will become clearer after seeing the example.
All the keysym
expressions are parsed before actually performing the changes, so xmodmap will inform you of any errors. The same keysym
can be bound to multiple keys, the expression is executed for each key. The remove expression is evaluated as it is found, so you don't have to worry about how are the keys assigned when you are removing a modifier. The add expressions, on the other hand, are parsed at the end, once all the reassignments have been executed.
In this first example, where I swap the Escape
key with Caps_Lock
, notice how I remove the Caps_Lock
lock before anything, reassign the keys, and finally re-add the Caps_Lock
lock. By the time the add expression is parsed, the physical Escape key is already working as the Caps Lock key, therefore, I use add Lock = Caps_Lock
and the physical Escape key gain the functionality of Caps Lock. I could use this expression in the first line and it would work just fine, however, I think it looks more understandable to have it as the last line.
!Swap Caps_Lock with Escape
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
keysym Escape = Caps_Lock
add Lock = Caps_Lock
Some other examples are:
!Turn Caps Lock into a second Escape key
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
!Turn Caps Lock into a second BackSpace key
remove Lock = Caps_Lock
keysym Caps_Lock = BackSpace
!Swap Caps Lock with the Left Control key
remove Lock = Caps_Lock
remove Control = Control_L
keysym Caps_Lock = Control_L
keysym Control_L = Caps_Lock
add Lock = Caps_Lock
add Control = Control_L
!Turn Caps Lock into another Control key
remove Lock = Caps_Lock
add Control = Caps_Lock
We can even add some extra functionality, e.g., if we wanted to turn the Caps Lock key into a Mute key (assuming that the desktop environment supports it), we can use:
!Turn Caps_Lock into a Mute key
remove Lock = Caps_Lock
keysym Caps_Lock = XF86AudioMute
If something goes wrong with your configuration, and, say, you lose the Control key, at the end of this post I cover how to return the functionality to the keys, and, of course, you can always delete the file and restart X/the computer, but this shouldn't be necessary. Once again, for a list of keys, we can check XKeysymDB
usually located in /usr/share/X11/XKeysymDB
.
After this we just need to run the following to apply the changes for this session:
xmodmap ~/.Xmodmap
Also noteworthy is the fact that we can run this expressions without the need of a file, which of course won't serve us for the purpose of making this change permanent. We use the -e
parameter in xmodmap, followed by the expression enclosed in double quotes.
xmodmap -e "keysym Escape = Caps_Lock"
Running the script when Fluxbox, KDE and X itself start
In Fluxbox we have the startup file located in ~/.fluxbox/startup
, we just need to edit this file and add the previous command, xmodmap ~/.Xmodmap
to the file. It may already have a commentary indicating where to add the command (# Change your keymap), but if it doesn't, we can put the command in a line between the lines #!/bin/sh
and exec fluxbox
To make the change when KDE or X starts, we need to create a script, which is just a text file:
touch ~/remapkeys.sh
We open it with our favorite text editor, and Inside we add the following lines:
#!/bin/sh
xmodmap ~/.Xmodmap
Then we give it execution privileges:
chmod u+x ~/remapkeys.sh
And now, we can move it to the autostart folder of KDE located in ~/.kde/Autostart/
if we want it to run when KDE starts:
mv remapkeys.sh ~/.kde/Autostart/remapkeys.sh
Or we can move it to ~/.config/autostart/
so it runs when X start:
mv remapkeys.sh ~/.config/autostart/remapkey.sh
Return keys to it's original function with xmodmap
When we use keysym
, we are effectively turning a key into another, and every time that we use said key as a parameter of keysym
, we are using whatever equivalences we defined previously. So, if we turn Caps Lock into another Escape key, we can not simply use something like Caps_Lock = [something]
in a subsequent command, because Caps_Lock
is no longer defined, and if we use Escape = Caps_Lock
we would be turning both physical keys into Caps Lock.
To return a key to a normal functionality, we need to use keycode
with xmodmap
, and we need the keycode of the key that we want to return to the normal functionality. So, lets say that Caps Lock is a second Escape key, the first step is to obtain the keycode of the physical Caps Lock key, we can do this with the command xev
.
We need to open a terminal and run xev
, we will see a small window appearing, this window will report in the terminal any X events that occur, such as key presses. Therefore, having this window selected, and the terminal from which we launched it visible, we press Caps Lock, and we will see something like this:
KeyPress event, serial 33, synthetic NO, window 0x6200001,root 0x1ad, subw 0x0, time 82765714, (426,194), root:(428,217),state 0x0, keycode 66 (keysym 0xff1b, Escape), same_screen YES,XLookupString gives 1 bytes: (1b) "XmbLookupString gives 1 bytes: (1b) "XFilterEvent returns: FalseKeyRelease event, serial 33, synthetic NO, window 0x6200001,root 0x1ad, subw 0x0, time 82765801, (524,192), root:(526,215),state 0x0, keycode 66 (keysym 0xff1b, Escape), same_screen YES,XLookupString gives 1 bytes: (1b) "XFilterEvent returns: False
The important part here is the keycode, in this case 66. We can use this keycode to restore the functionality of a key. If, for example, we turned Caps Lock into a second Escape key, we can type or copy this in a text file:
keycode 66 = Caps_Lock
add Lock = Caps_Lock
Then pass this file name as a parameter of xmodmap
, and the key's functionality should be restored.
The usage of xmodmap
as well as the mapping of the keyboard is an extensive subject to be covered in a single post, I expect to extend this, or find better ways to explain some of the concepts, as well as in the future create more about this subject. If something is not clear, or if there is any doubt please send it to contact@jveweb.net, I don't bite.
On another note, this is neither of the posts that I intended to publish this weekend, but one needs more images and the other needs further testing.