Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
Amazing. Changing relatively low-level behavior and yet so easy. A leap closer to a OS paradise. Hopefully there is even better, 100 % working solution, otherwise this is the answer. –  courteous Nov 11 '12 at 10:20
    
As you say, it doesn't work in all apps. Xcode, for one, won't budge to this .dict (but that can be "solved" with a custom keybinding, say, ⌘+B and ⌘+F). Still, very powerful to have Emacs-like behavior (almost) throughout the OS. –  courteous Nov 11 '12 at 15:38
    
KeyRemap4MacBook has an emacs mode group that changes for example ⌥F to ⌥→, but not when Terminal or Emacs is frontmost. You can customize it by creating a private.xml based on it. I think the real solution is to just get a keyboard with the arrow keys and modifier keys at better positions. –  Lri Nov 12 '12 at 19:36
1  
@courteous The emacs mode in KeyRemap4MacBook does that. It's not possible in a keylayout file or with DefaultKeyBinding.dict. –  Lri Nov 18 '12 at 14:52
1  
@courteous I added that to the answer as well. I have "~w" = selectWord:; in my DefaultKeyBinding.dict. –  Lri Nov 19 '12 at 5:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.