Emacs keybindings, PhpStorm, Mac OS, and the random characters I almost add to Drupal

By xjm, February 1, 2016

Drupal 8 developers, as a whole, are a bit gushy about PhpStorm. I use it; it's useful. But a decade and a half of my programmer muscle memory is Emacs keybindings, so when I use a different tool than Emacs, I feel like I'm trying to cut vegetables with a mallet, or write with a wad of chewed gum. And yes, PhpStorm ships with an Emacs keymap, but it's just not the same. It's not only a matter of the PhpStorm Emacs keymap being less cared for than its Vim cousin. It's that PhpStorm does not support M-x butterfly.

The most immediate hindrance in my Emacs-bound PhpStorm experience was actually my operating system's fault, though, not PhpStorm's. The meta key (or M-) is an essential part of using Emacs, and it's typical to use the Mac option key for meta because it is in a nice spot for Emacs-strong pinkies. In Mac OS generally, however, the option key is a modifier to type common special characters.

So my OS was actually taking over some keybindings even though PhpStorm would have supported them, and the result was that I could rarely go five minutes without inserting one of these characters into the code I was trying to look at:

ƒ ˘ ¯

(Yep, Views is even more fun to debug when you're trying to take a differential of the ViewExecutable.)

Those characters respectively correspond to deleting a word, moving back a word, moving forward a word, paging up, jumping to the beginning of the document, jumping to the end of the document, and the M-x key combination that is used to execute basically any command. So six of the most basic keyboard shortcuts for editing text, and then the one command to bring them all and in the darkness bind them. I had to stop using PhpStorm with my Drupal git repo that has push access, because several times I have very nearly committed such seemingly random inserts to Drupal core.

The last time I searched the internets, all the solutions to this problem seemed terribly complicated and generally to involve giving up the existing Mac OS functionality of my option key entirely. (Or using Esc as meta, which would be way more awkward.) A recent stackoverflow post, however, has a solution I wish I'd known about fourteen years ago. It actually just takes a dash of Cocoa to add additional Emacs-friendly bindings for Mac OS, and the nice folks at Apple even used Emacs bindings as their example. So now I have this file in ~/Library/KeyBindings/DefaultKeyBinding.dict:

/* ~/Library/KeyBindings/DefaultKeyBinding.dict */ 
  /* Additional Emacs bindings */
  "~f" = "moveWordForward:";
  "~b" = "moveWordBackward:";
  "~<" = "moveToBeginningOfDocument:";
  "~>" = "moveToEndOfDocument:";
  "~v" = "pageUp:";
  "~d" = "deleteWordForward:";
  "~^h" = "deleteWordBackward:";
  "~\010" = "deleteWordBackward:";
  /* Option-backspace */
  "~\177" = "deleteWordBackward:";
  /* Option-delete */
  "^_" = "undo:";

I can now navigate any application (not just PhpStorm) with the shortcuts that my fingers know best. I didn't even have to give up the corresponding special characters; it's as simple as just prefixing the key combination with a ctrl-Q to type one. This Cocoa selector reference also has all kinds of other nifty commands that you can bind.

I still get little signs instead of butterflies, and I still wish I could create macros on the fly as effortlessly as I can in Emacs. (I can bind keys to PhpStorm macros themselves, but have yet to figure out how to bind a key to start recording a macro. I have also tried recording a macro to start recording a macro, but PhpStorm did not give me the option to so recurse.) But it's a lot better than having to navigate a file with my arrow keys and mouse.

Handy tip. You can swap between the Emacs and Default PhpStorm keymaps with this key sequence:

ctrl-` 3 2

Default PhpStorm bindings

ctrl-` 3 4

Emacs bindings


I'd love to figure out how to make that an easier shortcut too.