n8blog
distraction in action

Like my work? Check out HexaLex, my game for iPhone & iPod Touch. It's a crossword game like Scrabble, but played with hexagonal tiles. http://www.hexalex.com

For some reason the Mac gods decided that Home and End scroll your window to the beginning and end of file respectively. This is just wrong. They should move the cursor to the beginning or end of the line it's on, just like they do in every other modern UI. Thankfully, there's a way to fix this for all Cocoa apps, which for me is everything I use!

First you need to make a subdirectory called KeyBindings in your ~/Library directory. Next, use your favorite text editor to create a file in that directory named DefaultKeyBinding.dict containing this text:

{
    "\UF729"  = "moveToBeginningOfLine:";                   /* Home */
    "\UF72B"  = "moveToEndOfLine:";                         /* End */
    "$\UF729" = "moveToBeginningOfLineAndModifySelection:"; /* Shift-Home */
    "$\UF72B" = "moveToEndOfLineAndModifySelection:";       /* Shift-End */
}

Restart any Cocoa apps (Safari, Mail, or TextEdit) to see the new keybindings take effect!

For more info on this method of binding keys see this OS X hint and this Apple document. The commands like moveToEndOfLine: are actually “Action methods” of the NSResponder Cocoa class.

  • Share/Bookmark

  Comments:

1. Danny Abesdris replies:

Hello,

Thank you for posting this help article on fixing the
home and end keys on the mac.
Much appreciated.
I was wondering how to determine the actual numeric keycodes
for these keys?
You see, I use a handy unix-based editor called “nled”
and would like to have the end/home keys work the same
way when I edit a file from the command line.
The end key seems unresponsive when editing a file within
a terminal window.
I tried capturing the keycode using getch( ); in ,
but nothing happens.
Is the “end” key only available for cocoa-based apps?

Thanks for any help/feedback.
…Danny

2. n8 replies:

Well, the “numeric codes” for keys in OS X are kind of hard to define.  There are a variety of different levels at which codes get assigned to keys — the driver level, the OS level, the UI level, X11, etc.  If you’re not able to use the home/end keys in your terminal program it may be due to the keyboard settings of the terminal.  If you’re using Terminal.app you should check the “Window Settings…” context menu under “Keyboard” to see what’s assigned to those keys.  In mine, end is assigned to Esc[f and I don’t have any problems.

It may also be due to the “terminal emulation” part of the equation.  In the Preferences dialog for Terminal.app, try selecting “xterm-color” for the terminal type.  Some programs and libraries use this information when deciding how to react to keystrokes.  If xterm-color doesn’t work, try plain xterm and/or a few other settings (be aware that these settings only take effect in terminal windows opened after you change them).

Hope that helps.  This terminal emulation business is a messy, messy aspect of computing — an unfortunate legacy of days when lots of people were trying to solve the same problem in mutually incompatible ways.  One would hope that by now it all would have been settled, but alas, we are not so lucky.

3. Richard Hornsby replies:

This thread is of great use … it really should be in a FAQ somewhere.  Having come from Linux, some of the default keybindings on OSX seem completely inane.

Anyways, I believe that the correct binding for the end key is

ESC[F  and not  ESC[f

This should appear as 33[F

The binding for the home key is 33[H

(If you look through the list of defaults, Shift+Home and Shift+End give you the behavior you want, but that seems kind of a dumb default, imo.)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Please type this word with the letters reversed: live

Like my work? Check out HexaLex, my game for iPhone & iPod Touch. It's a crossword game like Scrabble, but played with hexagonal tiles. http://www.hexalex.com