System-wide Readline Shortcuts for macOS with BetterTouchTool
macOS comes with system-wide Emacs-style shortcuts by default.1 For example:
Control-B: Move one character backward.
Control-H: Delete the character to the left of the insertion point. Or use Delete.
Control-K: Delete the text between the insertion point and the end of the line or paragraph.
…
These work in any text field. However, these are not good enough, because:
-
They’re only a subset of the readline bindings.2 In other words, the most useful Control+W (delete last word) is not there.
-
Control+B/F/P/N/H only works in text fields, while being immensely useful in many places outside of text fields. For example, selecting To-Do-List item in Things.app, navigating Google Sheets cells, or select candidate in the Chinese input method.
Enter BetterTouchTool! BetterTouchTool allows you to create keyboard shortcuts globally for each application, and it perfectly solves the problem.
Therefore, we can create bindings like the following:
Until, you find out that a bunch of key bindings in your Vim (or VimR, MacVim, etc.) stopped working. This is not surprising because now when you press Control+N, the keycode that iTerm2 receives is “Down”. Fortunately, we could create shortcuts local to one application to “shadow” the global bindings. In this case:
Use “Use Apple Default Or Do Nothing” to cancel out the global shortcut for iTerm2 (or anything else that actually needs those bindings).
Enjoy :)
Note: As of the time of writing (2019-02-24), BetterTouchTool is still suffering from a issue where shortcut switching sometimes lags behind application switching, so that sometimes the “shadowing” will not work and iTerm2 still gets Control+N as Down. See issue tracker. As a temporary workaround, in iTerm2, I manually mapped these bindings back to their control ASCII codes, e.g. “Option+Delete” -> “Control+W” (via its hex code 0x17, shown below).