1

Note: I asked this question in superuser about a month ago, but there hasn't been a reply till then, and the question relates to linux, so I'm posting it here.

I want to swap esc and caps_lock keys on my keyboard. setxkbmap -option caps:swapescape gets the job done but only for X. The keys behave in original way on a virtual console. How can I make them behave in the required way on a vc?

1

Use loadkeys.

To swap Esc and Caps Lock in the console, run

printf 'keycode 1 = Caps_Lock Caps_Lock\nkeycode 58 = Escape Escape\n' | sudo loadkeys -
  • Is it ok to set the sticky bit on loadkeys, so that I can put it in login script. Or shall I make another executable which calls loadkeys and set sticky bit on that. – saga Nov 29 '16 at 11:19
  • @Saga: Add saga ALL = NOPASSWD: /usr/local/bin/swap-esc-tab to /etc/sudoers (use e.g. EDITOR=nano visudo to edit the file with nano), create root-executable shell script /usr/local/bin/swap-esc-tab that runs the command shown in my answer, and finally add sudo /usr/local/bin/swap-esc-tab to your login script. – Nominal Animal Nov 29 '16 at 20:49
  • Note that when I have lots of such scripts, I tend to put the scripts that require root in /usr/local/admin-bin/, with simple exec sudo /usr/local/admin-bin/command "$@" wrapper bash/dash scripts in /usr/local/bin/, and add nominal-animal ALL = NOPASSWD: /usr/local/admin-bin/* to /etc/sudoers, so I can run those scripts as a normal user, only acquiring root for those specific scripts. – Nominal Animal Nov 29 '16 at 20:55
  • Nice setup, I think I'll do the same. – saga Nov 30 '16 at 4:38
2

Debian and console-setup

On Debian Linux and Debian FreeBSD, the design is for the X server and the kernel virtual terminals to share one set of configuration information. The console-setup package provides tools and startup scripts that take the keyboard/font configuration of the X server, convert it, and use it to configure the kernel virtual terminals.

Specifically: The console-setup service invokes the setupcon --save command, which generates scripts in /etc/console-setup containing the converted keyboard and font setup instructions, which are run against the kernel virtual terminal devices by udev rules. The keyboard map is converted from XKB to loadkeys format by ckbcomp, and the generated script invoked from the udev rule runs loadkeys.

So on Debian Linux and Debian FreeBSD you need to ensure that your XKBMODEL, XKBLAYOUT, XKBVARIANT, XKBOPTIONS variables are properly set in /etc/default/keyboard. In your case in particular you need to ensure that the XKBOPTIONS variable has caps:swapescape. Then you need to ensure that you have the console-setup package properly installed.

systemd Linux

On systemd operating systems, things are somewhat less integrated than on Debian. There's a configuration file named /etc/vconsole.conf and a service named systemd-vconsole-setup.service that processes it at system bootstrap (in response to a udev rule announcing the existence of the kernel virtual terminal subsystem). But this isn't joined up with the X server configuration at all.

The vconsole.conf file has a KEYMAP setting denoting the keyboard map that is passed to loadkeys, which again one does not need to explicitly run oneself. But it's left entirely up to you to create a (customized) keyboard map that swaps Caps Lock and Escape and put it where loadkeys can find it.

To get more joined up settings, one has to involve another two services, systemd-localed and the Desktop Bus. One runs, say,

localectl set-x11-keymap pl pc105 "" "caps:swapescape"
and the locale D-BUS service goes and rewrites /etc/vconsole.conf with its best guess as to the nearest equivalent map for the kernel virtual terminal. This nearest equivalent may not be exactly equivalent, though, and you may find it just outright ignoring options and suchlike.

Of course, you could always alternatively use Debian's ckbcomp by hand to directly convert an XKB keyboard map. ☺

Further reading

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.