Swapping Ctrl and Alt keys on a GNU Debian system

Tested with Debian 7, 8, 9, and 10.

if you're a touch typist you might notice that you use the Ctrl key much more often than Alt, and stretching the finger to Alt feels a little uncomfortable. One tip you can use to mitigate the uncomfortable feeling is to press the Ctrl key with a side of your palm, giving that your keyboard has sufficiently big and protruding buttons.

When it's not the case, especially true with a laptop keyboard, programmatically swapping the keys might be something you'd give a try. This way you can comfortably reach the Ctrl buttons with your thumbs.


Reminder: you normally enter TTY when pressing the Alt-Ctrl-1 combination

First you need to create a regular text file that will hold the key mappings and fill it with the following contents:

keymaps 0-127 keycode 56 = Control ! left alt to ctrl. keycode 100 = Control ! right alt to ctrl. keycode 29 = Alt ! left ctrl to alt. keycode 97 = Alt ! right ctrl to alt.
When you've done, save it on your system where you see fit, in my example it is in the /home/user/.config directory. I named the file Alt2Ctrl_TTY.map
To do the swapping run the following command:
sudo loadkeys $HOME/.config/Alt2Ctrl_TTY.map

Note that after rebooting your machine the functions of the Alt and Ctrl keys will be restored to the original state.

For the X Window System - X11 (GUI)

Also works for terminal emulators opened in X11

A similar procedure is used with the X Window System, create a file and put this into it:

clear control clear mod1 keycode 37 = Alt_L Meta_L keycode 105 = Alt_R Meta_R keycode 64 = Control_L keycode 108 = Control_R add control = Control_L Control_R add mod1 = Alt_L Meta_L

I've saved my file using this path: /home/user/.config/Alt2Ctrl_GUI.xmap

Run swapping by executing the following command, adjusting the filename or path if necessary:
xmodmap $HOME/.config/Alt2Ctrl_GUI.xmap
The http://xahlee.info/linux/linux_swap_control_and_alt_xmodmap.html web page was a source of my knowledge in this area some years ago.

With the xmodmap for X11 there's no need to be a superuser or use sudo.

As with TTY, after rebooting, the key functions will be restored to the original state.

Making the Remapping Convenient

Writing a shell function to encompass the swapping commands
swap-alt-and-ctrl-keys () { xmodmap $HOME/.config/Alt2Ctrl_GUI.xmap sudo loadkeys $HOME/.config/Alt2Ctrl_TTY.map 2>/dev/null }

Note that this function can remap both TTY and X11 in one go only when being run under the X Window System.

Swapping Ctrl and Alt keys on a GNU Debian system
This page was last updated on March 14, 2021
No comments have been posted so far
Leave a Comment