Emacsify OSX throughout: word-forward (⌥ + F) and word-backward (⌥ + B) instead of 'special characters'?

Yes, the OSX already supports + and + for moving the cursor word-backward and word-forward, but reaching for arrow keys is annoying. So, why not rather have +B and +F do this? I mean, not once have I (intentionally) wrote the integral sign ∫ with +B (apart from now).

In short, I'd rather have Emacs-like behavior (not just, say, ^+F for character-forward) throughout the OSX.


Related question: Stop using Mac keyboard Option as a modifier to character


Solution 1:

DefaultKeyBinding.dict

Create ~/Library/KeyBindings/ and save a property list like this as ~/Library/KeyBindings/DefaultKeyBinding.dict:

{
  "^ " = setMark:;
  "^/" = undo:;
  "^l" = selectParagraph:;
  "^u" = deleteToBeginningOfParagraph:;
  "^w" = deleteToMark:;
  "^x" = {
    "^x" = swapWithMark:;
    "^m" = selectToMark:;
  };
  "^V" = pageDownAndModifySelection:;
  "~@" = selectWord:;
  "~b" = moveWordBackward:;
  "~c" = (capitalizeWord:, moveForward:, moveForward:);
  "~d" = deleteWordForward:;
  "~f" = moveWordForward:;
  "~l" = (lowercaseWord:, moveForward:, moveForward:);
  "~r" = centerSelectionInVisibleArea:;
  "~u" = (uppercaseWord:, moveForward:, moveForward:);
  "~v" = pageUp:;
  "~w" = (deleteToMark:, setMark:, yank:, swapWithMark:);
  "~B" = moveWordForwardAndModifySelection:;
  "~F" = moveWordForwardAndModifySelection:;
  "~V" = pageUpAndModifySelection:;
}

DefaultKeyBinding.dict doesn't work in some applications like Xcode or Firefox. Methods that use the kill ring remove formatting from rich text. Key combinations that enter dead key states (like option-u in the U.S. keyboard layout) cannot be reassigned.

See also http://www.hcs.harvard.edu/~jrus/site/KeyBindings/Emacs%20Opt%20Bindings.dict. transposeWords: doesn't work in most applications. openDocument: and saveDocument: don't work in applications that use auto-save, and performClose: doesn't work everywhere either.

See http://www.hcs.harvard.edu/~jrus/site/cocoa-text.html or http://osxnotes.net/keybindings.html for more information.

KeyRemap4MacBook

One option is to enable settings in the predefined Emacs mode group:

Another option is to save a file like this as ~/Library/Application Support/KeyRemap4MacBook/private.xml:

<?xml version="1.0"?>
<root>
  <appdef>
    <appname>EMACSMODEIGNORE</appname>
    <equal>com.gnu.Emacs</equal>
    <equal>com.apple.Terminal</equal>
    <equal>com.googlecode.iterm2</equal>
    <equal>com.vmware.fusion</equal>
    <prefix>com.vmware.proxyApp.</prefix>
  </appdef>
  <item>
    <name>emacs</name>
    <identifier>private.emacs</identifier>
    <not>EMACSMODEIGNORE</not>
    <autogen>__KeyToKey__ KeyCode::P, VK_CONTROL | ModifierFlag::NONE, KeyCode::CURSOR_UP</autogen>
    <autogen>__KeyToKey__ KeyCode::N, VK_CONTROL | ModifierFlag::NONE, KeyCode::CURSOR_DOWN</autogen>
    <autogen>__KeyToKey__ KeyCode::B, VK_CONTROL | ModifierFlag::NONE, KeyCode::CURSOR_LEFT</autogen>
    <autogen>__KeyToKey__ KeyCode::F, VK_CONTROL | ModifierFlag::NONE, KeyCode::CURSOR_RIGHT</autogen>
    <autogen>__KeyToKey__ KeyCode::P, VK_CONTROL | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_UP, VK_SHIFT</autogen>
    <autogen>__KeyToKey__ KeyCode::N, VK_CONTROL | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_DOWN, VK_SHIFT</autogen>
    <autogen>__KeyToKey__ KeyCode::B, VK_CONTROL | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_LEFT, VK_SHIFT</autogen>
    <autogen>__KeyToKey__ KeyCode::F, VK_CONTROL | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_RIGHT, VK_SHIFT</autogen>
    <autogen>__KeyToKey__ KeyCode::B, VK_OPTION | ModifierFlag::NONE, KeyCode::CURSOR_LEFT, VK_OPTION</autogen>
    <autogen>__KeyToKey__ KeyCode::F, VK_OPTION | ModifierFlag::NONE, KeyCode::CURSOR_RIGHT, VK_OPTION</autogen>
    <autogen>__KeyToKey__ KeyCode::B, VK_OPTION | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_LEFT, VK_OPTION | VK_SHIFT</autogen>
    <autogen>__KeyToKey__ KeyCode::F, VK_OPTION | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_RIGHT, VK_OPTION | VK_SHIFT</autogen>
    <autogen>__KeyToKey__ KeyCode::D, VK_OPTION | ModifierFlag::NONE, KeyCode::FORWARD_DELETE, VK_OPTION</autogen>
    <autogen>__KeyToKey__ KeyCode::V, VK_OPTION | ModifierFlag::NONE, KeyCode::PAGEUP</autogen>
    <autogen>__KeyToKey__ KeyCode::V, VK_OPTION | VK_SHIFT | ModifierFlag::NONE, KeyCode::PAGEUP, VK_SHIFT</autogen>
    <autogen>__KeyToKey__ KeyCode::V, VK_CONTROL | VK_SHIFT | ModifierFlag::NONE, KeyCode::PAGEDOWN, VK_SHIFT</autogen>
    <autogen>__KeyToKey__ KeyCode::U, VK_CONTROL | ModifierFlag::NONE, KeyCode::DELETE, VK_COMMAND</autogen>
  </item>
</root>

Then open the KeyRemap4MacBook application, press the ReloadXML button, and enable the setting.

Change the key codes if you use a keyboard layout like Dvorak or Colemak.

See https://pqrs.org/macosx/keyremap4macbook/xml.html.en or http://osxnotes.net/keyremap4macbook.html for more information.