I'd like to exchange Esc and CapsLock in console (not in X, and use xev), how can I do it?
My OS is Ubuntu.
I'd like to exchange Esc and CapsLock in console (not in X, and use xev), how can I do it?
My OS is Ubuntu.
The tools to manipulate the keyboard layout on the virtual consoles are loadkeys, dumpkeys and showkey. Read their manpages and inform yourself about their intricacies.
Note that these tools only work in a virtual console, not in a terminal emulator in a graphical environment like gnome. The learn about the difference read this question and answers.
Here is a short guide to do what you want:
Save your current keyboard layout:
$ dumpkeys > backup.kmap
In case something goes wrong you might be able restore your keymap using the command:
$ sudo loadkeys backup.kmap
If the keyboard is so messed up that you can't even do this then your only option not involving ancient kernel magic is to reboot.
Check which keycodes are assigned to your keys:
$ showkey
Now press the ESC key and the CAPSLOCK key. The keycodes should show up on the screen. Note the keycodes. On my system the ESC has the keycode 1 and CAPSLOCK has the keycode 58. showkey will terminate after 10 seconds of inactivity (at least it does on my ubuntu 10.04).
Note the names of the ESC and CAPSLOCK keys from dumpkeys:
$ dumpkeys | grep 1
...
keycode 1 = Escape
...
$ dumpkeys | grep 58
...
keycode 58 = CtrlL_Lock
...
Note the keymap line from dumpkeys:
$ dumpkeys | head -1
keymaps 0-127
Create a keymap file which switches ESC and CAPSLOCK:
keymaps 0-127
keycode 1 = CtrlL_Lock
keycode 58 = Escape
Load the keymap:
$ sudo loadkeys swap_esc_capslock.kmap
Test: Testing the CAPSLOCK key is obvious. Just press they CAPSLOCK key and check whether other keys come out capitalized. To test the ESC key you can use CTRL+V followed by ESC. It should print ^[. CTRL+V makes the shell print the next key verbatim instead of interpreting it.
To have this modification load on every reboot, put the following line in your /etc/rc.local file:
/usr/bin/loadkeys /path/to/swap_esc_capslock.kmap
Information gathered from various pages, including, but not limited to:
Use ctrl:nocaps instead of ctrl:swapcaps if you just want to have two capslocks key (capslock by another name is still super useless).
X11: (see also: /usr/share/X11/xkb/rules/base.lst)
sudo vim /etc/default/keyboard
XKBOPTIONS="ctrl:swapcaps"
udevadm trigger --subsystem-match=input --action=change
sudo restart lightdm
Text console: (stolen from setupcon)
#!/bin/sh
. /etc/default/console-setup
. /etc/default/keyboard
ckbcomp $acm_option $rules_option -model "$XKBMODEL" \
"$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
| gzip -9 2>/dev/null >/etc/console-setup/cached.kmap.gz
loadkeys /etc/console-setup/cached.kmap.gz
On Ubuntu (22.04) the console keys are managed by the systemd service keyboard-setup (runs early - before login).
It is defined in /lib/systemd/system/keyboard-setup.service
(package: console-setup-linux).
The service will run /lib/console-setup/keyboard-setup.sh which will generate a new keymap with setupcon -k (or use a cached version).
Now setupcon (package: console-setup) is found in both /bin/setupcon and /usr/bin/setupcon; these are the same (shell scripts), given /bin --> /usr/bin.
Without a cached kmap.gz file, setupcon -k generates a gzipped keymap file in /etc/console-setup/. With a name that looks like: cached_.*.kmap.gz. E.g. I have: cached_UTF-8_del.kmap.gz.
The generated keymap file is loaded with loadkeys.
Configuration for setupcon is sourced from (both):
For setupcon -k, the relevant variables that these configuration files (shell scripts) set, (values are for me):
(Note: localectl set-x11-keymap ... writes /etc/default/keyboard.
If needed, read man localectl, there are commands to get the possible values for the XKB* variables.)
The configuration values are passed to /bin/ckbcomp, which is a perl script parsing all kind of X-files to generate the keymap (on stdout).
The ckbcomp script allows for user additions. It will add the contents of /etc/console-setup/remap.inc at the end of the generated keymap.
(Note: when CHARMAP is not UTF-8, compose.$CHARMAP.inc also gets included before remap.inc).
The remap.inc file has examples and is what you should be using if you want to make the mapping permanent.
So the answer to OP's question would be:
## run as root on a console
cd /etc/console-setup
{
echo 'keycode 1 = CtrlL_Lock'
echo 'keycode 58 = Escape'
} > remap.inc ## Keep the comments? Use '>> remap.inc'.
## Remove cached items, so setupcon -k generates a new keymap.
rm -f cached_*kmap.gz cached_keyboard_setup.sh
reboot
Debian also has the packages console-setup and console-setup-linux. So it probably works there too. I have not personally checked this works on Debian.