Tuesday, September 25, 2012

Fix the Home and End keys on Mac OS X

If you use a keyboard that's not designed specifically for Macs, you probably are familiar with the annoying mapping of the Home and End keys: they scroll to the beginning or end of an entire document, with no regard to the cursor's location.

Fortunately it's an easy fix. (Note: This works for native Cocoa apps only, not X11 programs.)

To get your Home and End keys working properly on Mac OS X (Tiger and up, including Mavericks), simply open the Terminal and do this:

$ cd ~/Library
$ mkdir KeyBindings
$ cd KeyBindings
$ nano DefaultKeyBinding.dict

Put these lines in that file, including the curly braces:

{
/* Remap Home / End keys to be correct */
"\UF729" = "moveToBeginningOfLine:"; /* Home */
"\UF72B" = "moveToEndOfLine:"; /* End */
"$\UF729" = "moveToBeginningOfLineAndModifySelection:"; /* Shift + Home */
"$\UF72B" = "moveToEndOfLineAndModifySelection:"; /* Shift + End */
"^\UF729" = "moveToBeginningOfDocument:"; /* Ctrl + Home */
"^\UF72B" = "moveToEndOfDocument:"; /* Ctrl + End */
"$^\UF729" = "moveToBeginningOfDocumentAndModifySelection:"; /* Shift + Ctrl + Home */
"$^\UF72B" = "moveToEndOfDocumentAndModifySelection:"; /* Shift + Ctrl + End */
}

Press Ctrl+O and then Enter to save the file, and Ctrl+X to exit. Restart your computer to have it take full effect.

Sources:
http://evansweb.info/2005/03/24/mac-os-x-and-home-end-keys
http://soodev.wordpress.com/2011/07/04/mac-os-x-remapping-home-and-end-keys/

6 comments:

  1. Including the currly braces or no?

    ReplyDelete
  2. Thanks, this worked but then I went to add some additional customizations to mimic the windows "CTRL + [ARROW KEY]" and after reboot it stopped working. So I removed my customizations and refollowed the steps above but now it won't work. Any idea what I might have done?

    ReplyDelete
    Replies
    1. Sorry, I don't know. Maybe one of the authors at either of the linked blogs could tell you better.

      Delete
  3. I figured it out, it's worth mentioning here that this only works with cocoa apps, which the Eclipse IDE and Firefox are not. Thanks for posting this!

    ReplyDelete