Most of my computing time is spent in Linux (with occasional time in Windows). On these platforms the Home and End keys universally jump to the beginning or end of the current line.

I am gradually getting used to using the one Apple computer in my life, but it is still frustrating than these keys seem to be bound to the beginning and end of the document and there doesn't seem to be any key at all for the line based actions.

My question:

  1. Are there existing keybindings I'm missing for beginning and end of line? (I mostly use a Terminal, Chrome and LibreOffice)
  2. Is there a way to rebind the functions of these keys to the functions I am familiar with? I am willing to give up the document based versions which seem of little use to me.

Also two caveats:

  1. I am not the only user on the system. It would be nice if my user settings didn't break other peoples experience. If this isn't possible, I think I probably have the strongest preference!
  2. I do use multiple languages and switch between keyboard layouts, so any solutions would be expected to work even when toggling between multiple keyboard layouts.
  • Note that while I ended up finding a solution and answering my own question, I'm open to easier or more versatile solutions. Also the process of discovering this is not clear. For example where do you find key codes and action names if I wanted to remap more keys? Feel free to add answers if you have relevant knowledge! – Caleb Jun 18 '11 at 9:08
up vote 262 down vote accepted

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.

  • 1
    Useful info about the key mappings - I've used these with the (commercial) BetterTouchTool, see this answer. – RichVel Sep 19 '17 at 8:43
  • 3
    This doesn't work in most applications in my experience. It is better to use Karabina-Elements. – Timmmm Oct 25 '17 at 8:56
  • 2
    This is great because it works in Terminal, iTerm2, Chrome, Sublime & Outlook. The alternative (Karabina) is less good because that maps "Home" to Command+LeftArrow, which actually switches windows in Terminal. You must remember to restart all apps after saving the .dict file. – Will Sheppard Nov 16 '17 at 15:14
  • This doesnt work with vim – MohitC Jun 1 at 7:08
  • /StandardKeyBinding.dict is binary. You can't read it – Green Jun 16 at 11:45

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) 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

  • 9
    Karabiner is great and also open source – Jedidja Feb 4 '15 at 17:14
  • 2
    With 10.10.5, the other suggestions seem to not work, but Karabiner (the new name for KeyRemap4MacBook -- works on all the Mac products) works well, and doesn't need a restart for the changes to take effect. Immensely helpful for this Mac noob; hopefully others can benefit as well. – Ralph J Nov 10 '15 at 15:36
  • 1
    As of today, "Karabiner does not work on macOS Sierra at the moment." – james.garriss May 19 '17 at 12:32
  • 2
    @james.garriss Give Karabiner-Elements a go - same author though rewritten from the ground up to support Sierra (it's still in development so they're quite a few rough edges) - github.com/tekezo/Karabiner-Elements – jklp May 21 '17 at 0:36
  • 4
    @jklp Unfortunately the functionality that is needed for remapping Home and End keys on Apple extended keyboard isn't yet available in Karabiner Elements. I personally seem to have the muscle memory for Ctrl-a, Ctrl-e when working in the console, but when working in a GUI app like Sublime, it's super difficult to remember. For Sublime (specifically) there is a solution, however: coderwall.com/p/upolqw/… – Ville Jun 19 '17 at 18:09

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.

I'm using OS X Sierra, and the ~/Library/KeyBindings/DefaultKeyBinding.dict solution suggested by other users partially worked: some apps like TextEdit now have the desired behavior, but other apps don't. Google Chrome and Slack are two that don't seem to respect the DefaultKeyBinding.dict file.

To fix the remaining apps, I used BetterTouchTool to create app-specific keyboard shortcuts binding Home and End to OS X's native shortcuts Ctrl+A and Ctrl+E. BetterTouchTool is not free, but there's a 45-day trial period.

To do this:

  1. Install and launch BetterTouchTool
  2. Go to the Gestures > Keyboard tab
  3. Under "Select Application", click + and choose the app you want to fix.
  4. Click "Add New Shortcut", assign the Home key in the "record shortcut" field, and set "Trigger other keyboard shortcut" to Ctrl+A. Repeat for End and Ctrl+E.
  5. When you're done the panel should look like this:BetterTouchTool panel
  6. Under BetterTouchTool's Advanced Settings, allow it to run on startup.

Note: don't quit the BetterTouchTool app or it will stop intercepting key presses. Just close its window and it should continue working in the background.

  • Thanks - just used this on El Capitan as I have already paid for BetterTouchTool, and it works fine with Chrome, Slack and other apps. – RichVel Sep 19 '17 at 8:41

I'm using the awesome KeyRemap4MacBook with the custom settings below.

It fixes Home/End in most applications and doesn't screw up iTerm2.

From https://gist.github.com/lhotari/6134223181439b9ed0a1

<?xml version="1.0"?>
<!-- ~/Library/Application Support/KeyRemap4MacBook/private.xml -->
<!-- Custom keyboard mappings for MBP using https://pqrs.org/macosx/keyremap4macbook/ -->
<root>
    <item>
        <name>Lari's PC Style Home/End</name>
        <appendix>(Change Home to Command+Left)</appendix>
        <appendix>(Change End to Command+Right)</appendix>
        <appendix></appendix>
        <appendix>{{ PC_STYLE_BINDINGS_IGNORE_APPS_DESCRIPTION }}</appendix>
        <identifier>lari.remap.pclikehomeend</identifier>
        <block>
            <only>DREAMWEAVER</only>
            <autogen>__KeyToKey__ FROMKEYCODE_HOME, KeyCode::HOME, ModifierFlag::SHIFT_L</autogen>
            <autogen>__KeyToKey__ FROMKEYCODE_END,  KeyCode::END,  ModifierFlag::SHIFT_L</autogen>
        </block>
        <block>
            <only>BLUEFISH,WORD,EXCEL,POWERPOINT</only>
            <autogen>__KeyToKey__ FROMKEYCODE_HOME, KeyCode::HOME</autogen>
            <autogen>__KeyToKey__ FROMKEYCODE_END,  KeyCode::END</autogen>
        </block>
        <!--
        <block>
            <only>EMACS, TERMINAL, X11, ECLIPSE</only>
            <autogen>__KeyToKey__ FROMKEYCODE_HOME, KeyCode::A, ModifierFlag::CONTROL_L</autogen>
            <autogen>__KeyToKey__ FROMKEYCODE_END,  KeyCode::E, ModifierFlag::CONTROL_L</autogen>
        </block>
         -->
        <block>
            <not>{{ PC_STYLE_BINDINGS_IGNORE_APPS }}</not>
            <autogen>__KeyToKey__ FROMKEYCODE_HOME, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_COMMAND, KeyCode::CURSOR_UP,    ModifierFlag::COMMAND_L</autogen>
            <autogen>__KeyToKey__ FROMKEYCODE_HOME, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_CONTROL, KeyCode::CURSOR_UP,    ModifierFlag::COMMAND_L</autogen>
            <autogen>__KeyToKey__ FROMKEYCODE_END,  MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_COMMAND, KeyCode::CURSOR_DOWN,  ModifierFlag::COMMAND_L</autogen>
            <autogen>__KeyToKey__ FROMKEYCODE_END,  MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_CONTROL, KeyCode::CURSOR_DOWN,  ModifierFlag::COMMAND_L</autogen>
            <autogen>__KeyToKey__ FROMKEYCODE_HOME,             KeyCode::CURSOR_LEFT,  ModifierFlag::COMMAND_L</autogen>
            <autogen>__KeyToKey__ FROMKEYCODE_END,              KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L</autogen>
        </block>
    </item>
</root>
  • Has not been updated to work with Sierra, per the link above. – james.garriss May 18 '17 at 14:35
  • This tool has been renamed to Karabiner and does apparently work with OS X El Capitan 10.11, Sierra 10.12 and High Sierra 10.13. – RichVel Sep 19 '17 at 8:40

I found the following useful for iterm2 and zsh with quotes from here:

Open iTerm > Preference. Go to profiles tab > Keys. Then click on the + button on the bottom of the keys list, press Home (or Fn+left arrow on the apple cordless keyboard), under Action select Send Escape Sequence, and then type [1~

Repeat for End key with [~4 for escape key sequence.

If you're using zsh, also do this:

1) Place the following in your ~/.zshrc file

bindkey "^[[1~" beginning-of-line
bindkey "^[[4~" end-of-line

2) Run source ~/.zshrc in any open terminals.

  • iTerm2 also has a "Natural text editing" key mapping preset that includes this behavior. – Matt M. May 1 '17 at 20:37
  • Needs to be changed to bindkey "^[[~4" end-of-line. Note the position of the 4. – Ty. Jun 15 '17 at 17:06
  • @Ty Thanks, but can you explain why? The original source, as well as github.com/reenhanced/zsh-config/blob/master/lib/… and github.com/ThiefMaster/zsh-config/blob/master/zshrc.d/… use "^[[4~" – RationalDev Jun 15 '17 at 17:25
  • I'm not well versed in these escape sequences but for me ^[[4~ echos out 4 while ^[[~4 moves the cursor to the end of the line. – Ty. Jun 15 '17 at 20:51
  • @Ty Interesting. I did some searching to figure out why, but I have only been able to find examples of ^[[4~. What kind of keyboard and character encoding are you using, maybe that makes a difference? – RationalDev Jun 16 '17 at 2:12

Imho the best tool to make keyboard changes is indeed Karabiner (old name KeyRemap4MacBook). Open source (https://github.com/tekezo/Karabiner) but also available as application (https://pqrs.org/osx/karabiner/index.html.en). I have been searching quite a long time and I have never find anything better.

You can also remap keyboard keys, as well as customize your mouse and trackpad in all kinds of cool ways, with the free BetterTouchTool, available at http://blog.boastr.net/. An excellent solution for customizing the workings of your input devices!

  • 1
    Could you expand your answer with instructions on how to use the specified software to solve the original problem? – grg Jun 11 '15 at 18:35
  • Not free in either the "Free Beer" or "Freedom" senses. 45-day trial, closed-source, paid software. – Wyatt8740 Aug 5 '17 at 5:43

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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