swapping keys in a virtual terminal

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

Use loadkeys.

To swap Esc and Caps Lock in the console, run

printf 'keycode 1 = Caps_Lock Caps_Locknkeycode 58 = Escape Escapen' | sudo loadkeys -


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x