12

Is there any way to disable CapsLock whenever Esc is pressed. Yes, I use VIM! I actually have an alternative all-caps keyboard layout on Ctrl-6 but I find it cumbersome and would prefer to just have the OS disable CapsLock whenever Esc is pressed. Also, I often SSH into other servers for a few hours so I'm on a stock VIM there.

Note that I happen to already have swapped the CapsLock and Esc keys. However, when the Esc key is pressed (which happens to be labeled CapsLock on the keyboard and is next to A), I need it to perform both Escape and Disable-CapsLock-If-It-Is-Enabled.

This is on Kubuntu 12.10. Thanks.

2
  • You need to add that functionality only for vim, or the complete desktop?
    – mtk
    Commented Jan 14, 2013 at 16:02
  • I would like it to be a desktop feature.
    – dotancohen
    Commented Jan 14, 2013 at 19:04

3 Answers 3

8
+50

In X11 (on console I don't know) you can do it by redefining the behaviour of the Escape key. I looked at the "shift(break_caps)" definition to see how it works, and adapted it.

Look at this answer on xkb for more details on how/where to put the locally modified files and load them.

And for doing what you want, you need in the local symbols file (eg: ~/.xkb/symbols/mysymbols) a section like this:

partial modifier_keys
xkb_symbols "esc_breaks_caps" {
    key <ESC> {
        type = "ALPHABETIC",
        actions [Group1] = [
            SetMods(modifiers=none),
            SetMods(modifiers=Lock,clearLocks)
        ]
    };
};

and in the local keymap file (eg: ~/.xkb/keymap/mykbd; you can create it with setxkbmap -print > ~/.xkb/keymap/mykbd ) change the xkb_symbols line to add +mysymbols(esc_breaks_caps).

You can now load it with: xkbcomp -I$HOME/.xkb ~/.xkb/keymap/mykbd $DISPLAY and pressing Esc will remove the CapsLock state (actually, the effect happens on the release of Esc; I think that only modifiers keys have immediate effect; others the effect is after their release.)

Oh, if you want to also swap Escape and CapsLock keys; then use this instead (and you put "+mysymbols(esc_swap_and_breaks_caps)" in your mykbd file):

partial modifier_keys
xkb_symbols "esc_swap_and_breaks_caps" {
    replace key <CAPS> {
        type = "ALPHABETIC",
        symbols = [ Escape, Escape ],
        actions [Group1] = [
            SetMods(modifiers=none),
            SetMods(modifiers=Lock,clearLocks)
        ]
    };
    replace key <ESC> { [ CapsLock, CapsLock ] };
};

note the physical keys are <CAPS> and <ESC>; <CAPS> (key engraved CapsLock in your keyboard) send Escape and <ESC> (key engraved Esc) sends CapsLock, whith <CAPS> (sending Escape) also unsetting capslock state

0
0

Add these lines to ~/.Xmodmap :

clear lock
keycode 0x42 = Escape

Then, type the command:

xmodmap ~/.Xmodmap

Your Caps Lock key is now remapped to the Esc key.

Reference url.


Also go through this nice article about Mapping CapsLock to Escape in Ubuntu, which seems to be a unix.se user :)

3
  • 1
    Thank you. I happen to already have swapped CapsLock and Esc. However, when the Esc key is pressed (which happens to be labeled CapsLock on the keyboard and is next to A), I need it to perform both Escape and Disable-CapsLock-If-It-Is-Enabled. I will edit the question to clarify. Thanks.
    – dotancohen
    Commented Feb 20, 2013 at 10:32
  • I thought you needed the other-way round. Somewhat confused here. What happens on pressing Esc and Capslocks after these changes ?
    – mtk
    Commented Feb 20, 2013 at 11:57
  • 1
    Currently: when pressing the ESC key the CapsLock function toggles, and when pressing the CapsLock key the ESC command is sent (i.e. the ESC key is remapped to CapsLock and the CapsLock key is remapped to ESC). My goal with this question is to have pressing the CapsLock key perform both the ESC function (which it currently does) and turn off the CapsLock function if and only if the CapsLock function is enabled (i.e. not toggle the CapsLock function, but rather disable it if CapsLock is enabled).
    – dotancohen
    Commented Feb 20, 2013 at 12:43
0

I'm not sure if this answers your question, but to map the Esc function to the CapsLock key, you can use this:

setxkbmap -option "caps:escape"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .