Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)

share|improve this question

2 Answers 2

up vote 23 down vote accepted

The tools to manipulate the keyboard layout on the virtual consoles are loadkeys, dumpkeys and showkeys. 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: http://askubuntu.com/questions/14284/why-is-a-virtual-terminal-virtual-and-what-why-where-is-the-real-terminal.

Here is a short guide to do what you want:

  1. 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.

  2. 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).

  3. Note the names of the ESC and CAPSLOCK keys from dumpkeys:

    $ dumpkeys | grep 1
    ...
    keycode   1 = Escape
    ...
    $ dumpkeys | grep 58
    ...
    keycode  58 = CtrlL_Lock
    ...
    
  4. Note the keymap line from dumpkeys:

    $ dumpkeys | head -1
    keymaps 0-127
    
  5. Create a keymap file which switches ESC and CAPSLOCK:

    keymaps 0-127
    keycode   1 = CtrlL_Lock
    keycode  58 = Escape
    
  6. Load the keymap:

    $ sudo loadkeys swap_esc_capslock.kmap
    
  7. 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:

share|improve this answer
    
The answer is excellent, thank you so much! –  Vayn May 29 '11 at 18:12
2  
You can also use sudo setupcon --save to make changes permanent, rather than editing rc.local. That's essentially the same as dumpkeys < /dev/tty1 | gzip > /etc/console-setup/cached.kmap.gz –  bukzor May 21 '12 at 2:21
    
It is safer to use /usr/bin/loadkeys in /etc/rc.localsince the path environment variable is not guaranteed to be set at this stage. And sudo is not needed in rc.local. –  August Karlstrom May 7 '13 at 20:04

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.