4

How can xkb or some other tool be used to permanently bind Caps Lock to ctrl+b while in terminal?

(This is to make Caps Lock the default prefix key for tmux. It could also be mapped to a specific key if that's too difficult, e.g. a function key, which could then be made the tmux prefix instead.)

  • remapping to a specific function key is certainly doable with XKB. F13 might be a good choice, since it won't already exist on most keyboards. remapping to a modifier key + regular key might be possible but would not be recommended. (consider: if you press A while holding down your ctrl+b capslock, should you get A or Ctrl+A?) – quixotic Oct 24 '17 at 23:49
  • F13 makes sense.. The only examples I've seen are showing how to do very specific things with Caps Lock e.g. disabling it or swapping it. Not arbitrary key mapping, will be glad if someone can show an example for that. – mahemoff Oct 25 '17 at 0:11
  • As for the caution, I don't think it's an issue in practice as I've used Caps Lock as global tmux prefix for some years on MacOS. I just release Caps Lock before hitting the next key. – mahemoff Oct 25 '17 at 0:13
2

XKB will be appropriate for Xwindows or Wayland GUIs. It will not affect virtual consoles, but GUI terminal emulators will be fine. For XKB background I'll point you to some (overview, system vs user) .. other (custom options) .. answers (custom rules).

The following will allow you to add a new option like caps:myf13 to an existing XKB layout with whatever tools you'd normally use (setxkbmap, localectl settings, GNOME panel, etc).


Defining the option

Existing XKB capslock options are listed in /usr/share/X11/xkb/rules/evdev.lst. Looking at the corresponding options in the .../rules/evdev file, you can see these options are all loaded from the file .../symbols/capslock. All of them are modifier keys, which probably aren't the best example, but caps:backspace might be a good comparison. Looking at the file, we find the stanza defining this option:

hidden partial modifier_keys
xkb_symbols "backspace" {
    key <CAPS> { [ BackSpace ] };
};

grep'ing through the other symbol files, we can see that the F13 symbol is simply F13. The new option stanza might look like this:

hidden partial modifier_keys
xkb_symbols "myf13" {
    key <CAPS> { [ F13 ] };
};

As you can see, we only changed the name of the option and the symbol assigned to the key.


Hooking it up

The only thing left to do is hook up the new stanza. On a basic Xwindows system, using commandline tools like setxkbmap and xkbcomp, a custom user location will do fine; for GNOME, KDE or a Wayland system you'll need to make your changes in the system XKB database.

As an example for system changes (you will need sudo access to create or edit these files):

  • Place the custom stanza in a new symbol file, eg /usr/share/X11/xkb/symbols/mycaps.

  • Add this to /usr/share/X11/xkb/rules/evdev just below the line for caps:backspace:

      caps:myf13   =   +mycaps(myf13)
    
  • ... add to /usr/share/X11/xkb/rules/evdev.lst:

      caps:myf13      Caps Lock is F13
    
  • ... add to /usr/share/X11/xkb/rules/evdev.xml:

          <option>
            <configItem>
              <name>caps:myf13</name>
              <description>Caps Lock is F13</description>
            </configItem>
          </option>
    
  • Finally, make backups of your .../rules/evdev* files, or create a patch file. Your changes will be overwritten whenever the xkb-data package is updated. If you saved your modification stanza into the .../symbols/capslock file, it will need to be backed up as well.

Once these changes are made, you should be able to set this option as if it were any other XKB option. You may need to restart any GNOME/KDE session for control panels to pick up the changes, but tools like setxkbmap should find it immediately: setxkbmap -option caps:myf13

  • Thanks for the step-by-step! Can you elaborate why in rules/evdev you're using mycaps(myf13) versus capslock(myf13)? – Raymond Kroeker Dec 17 '18 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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