2

I am using

  • arch kernel 5.7.3-arch1-1, and
  • openbox, and
  • neo vim 0.4.3, and
  • Lenovo thinkpad T430 laptop / keyboard.

All system packages are up to date.

I am trying to swap caps lock and escape keys for use in neoVim and have used this line

setxkbmap -option caps:swapescape

This sets the escape key as caps toggle as expected, but does nothing to the caps lock key. I'm expecting that the caps lock key will work as an escape key but it does not.

Can anyone indicate how I can do this, and why the caps lock does not work using the above command?

My understanding is that this cannot be done in nvim's .vimrc file.

Below is xev output when pressing and then releasing escape, keycode 9

KeyPress event, serial 48, synthetic NO, window 0x3200001,
    root 0x139, subw 0x0, time 75547017, (621,536), root:(792,564),
    state 0x0, keycode 9 (keysym 0xff1b, Escape), same_screen YES,
    XLookupString gives 1 bytes: (1b) "
mbLookupString gives 1 bytes: (1b) "
FilterEvent returns: False

KeyRelease event, serial 48, synthetic NO, window 0x3200001,
    root 0x139, subw 0x0, time 75547079, (621,536), root:(792,564),
    state 0x0, keycode 9 (keysym 0xff1b, Escape), same_screen YES,
    XLookupString gives 1 bytes: (1b) "
FilterEvent returns: False

Below is xev output when pressing and releasing caps lock, keycode 66

FocusOut event, serial 48, synthetic NO, window 0x4800001,
    mode NotifyGrab, detail NotifyAncestor

FocusIn event, serial 48, synthetic NO, window 0x4800001,
    mode NotifyUngrab, detail NotifyAncestor

KeymapNotify event, serial 48, synthetic NO, window 0x0,
    keys:  57  0   0   0   0   0   0   0   4   0   0   0   0   0   0   0   
           0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   

KeyRelease event, serial 48, synthetic NO, window 0x4800001,
    root 0x139, subw 0x0, time 75680956, (272,352), root:(443,380),
    state 0x2, keycode 66 (keysym 0xffe5, Caps_Lock), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False
2
  • Hmm... You don't get a KeyPress event for Caps_Lock, but do get a KeyRelease. Something seems out of place. If you investigate dmesg warnings and errors, do you find some related to the keyboard?
    – Quasímodo
    Commented Jun 25, 2020 at 14:00
  • @Quasímodo, thanks. I had a look in dmesg and in journalctl and could see no entry created by either setxkbmap -option caps:swapescape or xmodmap -e "keycode 9 = Caps_Lock"; xmodmap -e "keycode 66 = Escape", suggested below, which has the exact same effect, i.e., the escape key is remapped as caps lock, but the caps lock key is mapped to nothing. I used diff file1 file2 where file1 and file2 are outputs of all of journalctl and file1 is before the re-mapping and file2 is after the re-mapping. I am using a Lenovo T430 thinkpad.
    – Kes
    Commented Jun 27, 2020 at 12:54

2 Answers 2

0

this can be done with xmodmap. Swap the keys with.

xmodmap -e "keycode 9 = Caps_Lock"; xmodmap -e "keycode 66 = Escape"

afterwards this effect can be undone with

xmodmap -e "keycode 66 = Caps_Lock"; xmodmap -e "keycode 9 = Escape"

Not sure if this fits your needs, because it effects all processes within the x session.

4
  • Can you confirm this will transfer the modifying behavior of CapsLock to the new key too? I suspect xmodmap does not do that unless explicitly told so.
    – Quasímodo
    Commented Jun 25, 2020 at 13:59
  • Hi @rudd, thanks. @Quasímodo, thanks. The xmodmap -e "keycode 9 = Caps_Lock"; xmodmap -e "keycode 66 = Escape" keymapping has the exact same effect as setxkbmap -option caps:swapescape, i.e., the escape key is remapped as caps lock, but the caps lock key is mapped to nothing. I am using a Lenovo T430 thinkpad.
    – Kes
    Commented Jun 27, 2020 at 13:00
  • @Quasímodo, the command, xmodmap -e "keycode 9 = Caps_Lock"; xmodmap -e "keycode 66 = Escape" does exactly the same as teh command I have been using i.e., setxkbmap -option caps:swapescape. The escape key is re-mapepd as the caps lock key but the caps lock key is re-mapped as nothing. Both these commands do exactly the same thing on my Lenovo T430 keyboard.
    – Kes
    Commented Jun 27, 2020 at 13:03
  • setxkbmap -option caps:swapescape works perfectly for me, this answer does not. Then the caps key triggers both caps and escape.
    – xeruf
    Commented Nov 15, 2021 at 11:19
0

keyd is a universal way to rebind keys, but it doesn't have a way to rebind keys for specific programs. (It does have an experimental keyd-application-mapper, but it's only for windowed applications on specific window managers.) What you suggest would be impossible, or at least imperfect, as escape would be rebound to capslock whenever neovim is open, even if it's not currently focused/in use.

That said, keyd is "universal" because it works regardless of the session - X11, wayland, even TTY. I've been using it with no issues for months now.

To install and configure it, see the GitHub (linked above) or you can run these commands (this assumes you're using systemd):

cd
git clone https://github.com/rvaiya/keyd
cd keyd
make
sudo make install

Now edit /etc/keyd/default.conf and paste the following:

[ids]

*

[main]

escape=capslock
capslock=escape

Then enable and start the keyd service:

sudo systemctl enable --now keyd

You must log in to answer this question.

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