Friday 28 April 2017

Use Vim Mode on macOS

Karabiner is a very good keyboard customizing tool for Mac. I’ve been using it for a long time. I loved its Vim mode so much, I even bought a keyboard without arrow keys for my daily use.

However, Karabiner was broken after upgrading to macOS Sierra, due to the complete rewrite of the Mac keyboard driver. (Apple has done this to support the new Touch Bar MacBook Pro, apparently.)

Since then, the author of Karabiner has been working hard to reimplement the app. It is an open-source project on GitHub. I check the commits page every day to see the progress.

After almost one year, the new Karabiner still hasn’t been finished. While waiting for the new Karabiner, I’ve found that Apple actually added some native custom key binding support for macOS. Now it is possible to simulate some kind of the Vim mode on Mac.

Here’s what I’ve done:

  • Create a new file ~/Library/KeyBindings/DefaultKeyBinding.dict

  • Add these contents to the file

/* ~/Library/KeyBindings/DefaultKeyBinding.dict */

    /* Vim arrow keys */
    "^h" = "moveBackward:";  
    "^l" = "moveForward:";  
    "^k" = "moveUp:";  
    "^j" = "moveDown:";  
  • Reboot

Now I can use Ctrl-hjkl as system-wide arrow keys.

This method is however not perfect. If an app has defined its own use of Ctrl-hjkl keys, this setting will not overwrite it. So it just won’t work in some apps. It’s also not as convenient to press as the Karabiner Vim mode, which uses s key and a time delay instead of Ctrl.

I’m still waiting for the new Karabiner. Please consider donating to the author if you are waiting too.