Optimize for happiness

Life is easier with simple scripting. Grateful for everyone's encouragement and support!

View this project on GitHub

How else to make my keyboard my own

2023.06.14   •   3 min read

I have been thinking, what are the native system options to safely edit and create new functionality on a Mac keyboard. This led me to creating custom key bindings.

In essence, your computer keeps its shortcuts in binary format in this location:

/System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict

Do you want to see what’s inside? Issue this command:

$ plutil -convert xml1 /System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict -o -|pl|grep -v noop:|ruby -pe'$_.gsub!(/[^ -~\n]/){"\\U%04x"%$&.ord}'

The outcome may look weird, but let’s just look at an example line, for now:

"^d" = "deleteForward:";

Did you know that your keyboard can delete in the forward direction if you press control + d? That comes from that line above. To be clear, that is a control and not a command () key. (Also, by default, the forward delete shortcut is fn + delete.)

It’s a bad idea to mess with that global dictionary file, but we can create a user-specific key dictionary and add new or over-write the default shortcuts.

If you want to do that, your personal file has to be exactly this:

~/Library/KeyBindings/DefaultKeyBinding.dict

Chances are, you do not yet have the directory KeyBindings in your Library. Create it first.

My DefaultKeyBinding.dict file is simple, I wanted to create more ways to forward delete. So I assigned F5 to that (an otherwise unoccupied function key as I do not use Dictation). Also, shift - delete will delete forwards, because, why not. Other combinations are already used by the system:

  • alt - delete deletes the whole word going backwards;
  • command () - delete deletes from the cursor position to the start of the line.

Here is my example dictionary file, hosted on GitHub:

The key bindings are housed within {}, the rest are comments. I find it useful to explain what the symbols (key modifiers) mean, as well as include a list of key codes. All of those codes are defined by unicode apple mappings.

I added some notes on the programs where I ran into peculiarities with my key bindings. In particular, Xcode and Terminal can be picky. For example, F5 already had a custom (useless) assignments in both Xcode and Terminal so my keybinding was ignored. Since I removed those custom F5 functions from both Xcode and Terminal, the shortcut works great.

It would be really neat to specify a left or right option key in these bindings, but it seems to be outside the scope of Apple programming as it is currently documented.