Apple - Remap "Home" and "End" to beginning and end of line

The default shortcuts for moving to beginning or end of (wrapped) lines are and . and or A and E move to the beginning or end of unwrapped lines (or paragraphs). and move backwards/forward by words, and all of these are compatible with holding Shift to select during the corresponding moves.

You could remap home and end by creating ~/Library/KeyBindings/ and saving a property list like this as DefaultKeyBinding.dict:

{
    "\UF729"  = moveToBeginningOfLine:; // home
    "\UF72B"  = moveToEndOfLine:; // end
    "$\UF729" = moveToBeginningOfLineAndModifySelection:; // shift-home
    "$\UF72B" = moveToEndOfLineAndModifySelection:; // shift-end
}

Most of the keybindings for editing text in OS X are defined in /System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict.

Applying changes requires reopening applications. DefaultKeyBinding.dict is ignored by some old versions of Xcode (works with latest version 6.3.1), Terminal, and many cross-platform applications.

See Cocoa Text System and my website for more information about the customizable keybindings.

Terminal's keybindings can be customized in Preferences > Settings > Keyboard. \033OH moves to the beginning of a line and \033OF to the end of a line.

In Eclipse, key bindings should be modified in Preferences > General > Keys. You need to modify default bindings for commands Line Start and Line End (replace ⌘← by ↖ and ⌘→ by ↘). For selection to work, also modify Select Line Start and Select Line End.

PS: You may need to logout and login again for the ~/Library/KeyBindings/DefaultKeyBinding.dict change to take effect.


I'm currently on Lion 10.7.4, and for some strange reason editing the ~/Library/KeyBindings/DefaultKeyBinding.dict didn't work for me.

I have found that the KeyRemap4MacBook System Preferences addon (which is now called Karabiner-Elements) works really well, and you can also re-map a variety of keys with it, without having to edit any text files. E.g. changing the page up / page down keys so the cursor also follows on page up / down.

Just download the addon, and in the System Preferences panel, browse to the For PC Users section, and check Use PC Style Home /End.

Note, I picked (Change Home to Command+Left) instead of (Change Home to Control+A), as if I picked the first, and if the line was wrapped, home would take me to the beginning of the wrapped line, several lines above, instead of taking me to the beginning of the current line.

KeyRemap4MacBook


Thanks to this article, I figured out how to re-bind the Home and End keys to line based actions on a per-user basis that works across keyboard layouts.

You need to create a dictionary file with the keycodes and new commands to map to, but first make sure the folder where this will be placed exists by opening a Terminal and running:

$ mkdir -p ~/Library/KeyBindings

Then create or edit the file at ~/Library/KeyBindings/DefaultKeyBinding.dict. If it didn't exist before, add the whole section below. If it does exist, just add the four new rows inside the block.

{
    "\UF729"  = "moveToBeginningOfLine:";
    "\UF72B"  = "moveToEndOfLine:";
    "$\UF729" = "moveToBeginningOfLineAndModifySelection:";
    "$\UF72B" = "moveToEndOfLineAndModifySelection:";
}

In order to activate the settings you will need to logout of your user and login again.

Note that this covers both the normal action of moving the cursor and the Shift+Home/End combination to select durring cursor movement.

Tags:

Macos

Keyboard