1

I already understand how to remap keys in an X graphical server. However, I sometimes like to do work from a TTY and cannot seem to figure out a way to functionally remap Ctrl to Caps Lock.

I have already tried creating a .keymap file with the following contents:

keycode 37 = Control_L

Then sourcing it using loadkeys, but to no avail.

However, 37 is the keycode for Ctrl in X (according to xmodmap -pke | grep 'Control') so perhaps the keycode is different in a TTY? If so, how would I go about determining the correct key code and subsequently setting the remap?

2 Answers 2

2

The answer above by porkfreezer is great! To make this a permanent solution, you could make a .settings directory, or use your .local directory, to create your own keymap file like you were trying to do, that can be used when logging into a TTY. To do this, I recommend copying the keyboard keymap you are currently useing (for me it's us.map.gz) to your home directory that you chose above, for me this looks like

cp /usr/share/kbd/keymaps/i386/qwerty/us.map.gz ~/.settings/

Then you can unzip and edit that file to contain the key mapping you want. For me I have switched the Escape key and Caps_Lock, for use with vim. Then create a symlink to this file to a directory where Linux can find it, like so:

sudo ln -s ~/.settings/custom.map /usr/share/kbd/keymaps/i386/qwerty/custom.map

Now, when in a TTY, you can run sudo loadkeys custom to load the key mapping. To have this as the default, you could create a

/etc/vconsole.conf

file containing the line

KEYMAP=custom

Then edit your /etc/mkinitcpio.conf file (you will need superuser privileges) by adding keymap to the end of the hooks list, this should look something like the following

...
HOOKS=(base udev autodetect modconf block filesystems keyboard fsck keymap)
...

Then run

sudo mkinitcpio -p linux

And that's it! After a reboot, whenever you log into a TTY, your custom keymap will be used.

Hopfully this helps!

1

Kernel keycodes are different than X keycodes. Key 37 for the kernel is actually K (on qwerty). To find kernel keycodes you can use showkey:

$ showkey
kb mode was UNICODE
[ if you are using this under X, it might not work
since the X server is also reading /dev/console ]

press any key (program terminates 10s after last keypress)...
keycode  28 release    # I released enter
keycode  29 press      # I pressed control
keycode  29 release    # I released control
keycode  58 press      # I pressed caps lock
keycode  58 release    # I released caps lock

So, to map Ctrl to Caps Lock:

keycode 29 = Caps_Lock

Or Caps Lock to Ctrl:

keycode 58 = Control
1
  • I mapped Ctrl to the physical caps lock position by using keycode 58 = Control in my .keymap file but I had to use Bradon's solution otherwise whenever I pressed Ctrl it would lock the Ctrl function for all subsequent key presses and make the keyboard unusable.
    – se7enge
    Jul 11, 2022 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.