Ditching Karabiner and keyboard customizations

Blog Post

In the past, I've used Karabiner-Elements to customize my keyboard in peculiar ways. For instance, holding the keys s and d would trigger a hyper key to be captured by Hammerspoon to change applications. Eventually, I realized these customizations were unreliable and started finding different ways to improve my workflow. So for these past years, I only used three modifications in Karabiner:

  1. Caps Lock becomes Left Control.
  2. Right Command becomes Control + A when pressed.
  3. Right Command becomes Right Control when held.

Number 1 can already be accomplished in macOS keyboard preferences.

Number 2 was a single key modifier for tmux, but it is no longer useful. I changed tmux's prefix from Control + B to Control + A, which is much more comfortable. Still, with this, instead of doing Control + A and then V to enter visual mode, I would be able to do Right Command and V. It doesn't sound like a lot, but hitting Control + A all the time can be very frustrating. Since then, I changed to using Command as my modifier for tmux because it's a comfortable key to hit that doesn't serve any purpose on terminal applications. For Alacritty, you can use the command xxd -psd to figure out the hex codes for a shortcut, and then use those in Alacritty's configuration:

key_bindings:
  - { key: C,        mods: Command,        chars: "\x01\x63" } # sends Ctrl + a
  - { key: S,        mods: Command,        chars: "\x01\x73" } # sends Ctrl + s

You can map anything to a Command shortcut while keeping the default tmux shortcuts untouched.

That leaves me with number 3, a remap I did beacuse of Neovim, so I can use Control with either hand, depending on the other key I'm reaching for. I found out recently, somewhere on a Hacker News comment, that this customization can be accomplished using macOS's command line tool hidutil. Set the remaps you want in this web app, download the generated file to ~/Library/LaunchAgents/com.local.KeyRemapping.plist, and restart your system. That's it. You don't need to install anything to accomplish this.

As an extra, for those using ansible to set up their machines, I added two tasks: the first is to symlink the file into the correct folder, and the second is to remember that I need to reboot for it to take effect.

- name: link com.local.KeyRemapping.plist
  ansible.builtin.copy:
    src: "/roles/keyboard/files/com.local.KeyRemapping.plist"
    dest: "/Library/LaunchAgents/com.local.KeyRemapping.plist"
    force: false
  register: key_remapping

- name: reboot
  ansible.builtin.debug:
    msg: You need to reboot for KeyRemapping to take effect
  when: key_remapping.changed

I don't have anything against Karabiner, but it's a complex application that sits on my system, is weird to configure, and sometimes annoys me by asking for updates. This makes my setup simpler.

Reply via email