7

I'm looking for a way to swap Esc and Caps Lock on Linux virtual console.

In X11, I can do this with setxkbmap -option caps:swapescape, but I don't know an equivalent in text mode. So, what can I do?

Toby Speight
  • 8,678
fauve
  • 1,228

3 Answers3

8

First You Need To install "console-data"

sudo apt-get install console-data

Now use "sudo showkey" to find the keycode of your ESC and CapsLock key

sudo showkey

My keycode for ESC was "1" and for Caps Lock was "58"

Now you need to create a .keystrings file in your home directory

vim ~/.keystrings

In that file you'll swap the keycode's for Caps Lock and ESC ex. since the Caps Lock Key was equal to 58 before I'll make it equal to 1

keycode 1 = Caps_Lock
keycode 58 = Escape

Now Save and close your .keystrings file and run the following command in your TTY

sudo loadkeys .keystrings 

The Caps Lock and Escape keys should now be swapped

1

For Debian based systems, the procedure is quite straightforward. Look for the line XKBOPTIONS= in /etc/default/keyboard and change it to

XKBOPTIONS="caps:swapescape"

Root access will be required, of course. And don't worry, despite the "X" the console should have Esc and Caps Lock reversed upon reboot.

Read man keyboard and Changing the keyboard layout/mapping on both the console (tty) and X in an X/console agnostic way? for more information.

Quasímodo
  • 18,865
  • 4
  • 36
  • 73
1

Step by step solution

X11

The following command sets the X11 keyboard correctly (de is my keyboard layout):

localectl --no-convert set-x11-keymap de "" "" caps:swapescape

tty

The tty keyboard can be configured as follows:

  1. Copy the correct keyboard layout file to a custom directory:

cp /usr/share/kbd/keymaps/i386/qwertz/de-latin1.map.gz /usr/local/share/kbd/keymaps

(You will have to change that command depending on what keyboard layout you are using)

  1. Change every occurency of Escape to Caps_Lock and every occurency of Caps_Lock to Escape in the copied file and rename it to something like de-latin1-custom.map.gz

  2. Edit the /etc/vconsole.conf file to look like this:

KEYMAP="/usr/local/share/kbd/keymaps/de-latin1-custom.map.gz"

After rebooting the system the keyboard should be configured correctly for X11 and for tty.