August 09, 2013

Custom keybindings on OSX

This is mostly just a reminder post so that I don’t forget how to do this again but I guess some other people might find it useful so here goes:

You can create custom key bindings in OSX by defining them in a file located at ~/Library/KeyBindings/DefaultKeyBinding.dict. Note that’s DefaultKeyBinding and not DefaultKeyBindings, plural. That’s an annoyance that tripped me up for a few minutes!.

The contents of this file is a hash where the key is the keypress and the value is the action. This is probably best served by an example, so here’s mine!

{
  "~H" = (insertText:, "♥");
  "~T" = (insertText:, "(╯°□°)╯︵ ┻━┻");
  "~B" = (insertText:, "┬─┬ノ( º _ ºノ)");
  "~R" = (insertText:, "(ノಠ益ಠ)ノ彡┻━┻");
}

There are a few magic characters to be aware of: the one I am using ~ means the option (alt) key, there are others. For instance: ^ means Ctrl. The docs are here.

As you can see - I take my custom keybindings seriously. These memes come in exceptionally handy in git commit messages and IM chats!