Keyboard Shortcuts – Make Backspace Act as Escape Using Setxkbmap

keyboard shortcutsxkb

There seem to be several options for setxkbmap such as -option caps:backspace which makes caps a backspace. However I cannot seem to find an option that makes backspace an escape key. How do I create a single setxkbmap command that changes the backspace key to an escape key?

Best Answer

You'll have to define a new option.
First, make a new symbol file e.g. /usr/share/X11/xkb/symbols/bksp with the following content:

partial alphanumeric_keys
xkb_symbols "bksp_escape" {
    key <BKSP> { [ Escape ] };
};

Then create the new option like this:

bksp:bksp_escape  =   +bksp(bksp_escape)

(where bksp is the name of the symbol file and bksp_escape is the group name that was defined in this file) and add it to the options list in the rules set you're using - assuming evdev - so place it in /usr/share/X11/xkb/rules/evdev under ! option = symbols:

! option    =   symbols
  bksp:bksp_escape  =   +bksp(bksp_escape)
  ...........
  grp:shift_toggle  =   +group(shifts_toggle)
  altwin:menu       =   +altwin(menu)

Add it also to /usr/share/X11/xkb/rules/evdev.lst (with a short description) under ! option (e.g. right before ctrl):

! option
  ........
  bksp                 Backspace key behavior
  bksp:bksp_escape     Backspace as Escape
  ctrl                 Ctrl key position
  ctrl:nocaps          Caps Lock as Ctrl

You can then run, as a regular user:

setxkbmap -layout us -option bksp:bksp_escape

to enable the option and make BKSP behave as ESC.
You can also verify if:

setxkbmap -query

reports:

rules:      evdev
model:      pc104
layout:     us
options:    bksp:bksp_escape

and if

setxkbmap -print

outputs:

xkb_keymap {
    xkb_keycodes  { include "evdev+aliases(qwerty)" };
    xkb_types     { include "complete"  };
    xkb_compat    { include "complete"  };
    xkb_symbols   { include "pc+us+inet(evdev)+bksp(bksp_escape)" };
    xkb_geometry  { include "pc(pc104)" };
};

In Gnome 3 you can make the option permanent via dconf (or gsettings in terminal) e.g. add 'bksp:bksp_escape' to the org​>gnome​>desktop​>input-sources​>xkb-options key (note that in dconf values are separated by comma+space).


Finally, note that both evdev and evdev.lst will be overwritten on future upgrades (but not your custom bksp symbol file) so you'll have to edit them again each time the package that owns them is upgraded (on archlinux it's xkeyboard-config). It's easier to write a script that does that, e.g.

sed '/! option[[:blank:]]*=[[:blank:]]*symbols/a\
  bksp:bksp_escape  =   +bksp(bksp_escape)
' /usr/share/X11/xkb/rules/evdev

sed '/! option/a\
  bksp                 Backspace key behavior\
  bksp:bksp_escape     Backspace as Escape
' /usr/share/X11/xkb/rules/evdev.lst

If you're happy with the result use sed -i (or -i.bak if you want to make backup copies) to actually edit those files in-place.

Related Question