571

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.
3
  • 2
    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
    Commented Jun 18, 2011 at 9:08
  • 8
    I hate my mac for this exact reason. Every system uses home and end except mac. Commented Jun 26, 2021 at 19:53
  • 3
    this has been my biggest bane since using Mac
    – Eman
    Commented Jul 15, 2021 at 18:07

13 Answers 13

511

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 for more information about the customizable keybindings.

Terminal's keybindings can be customized in Preferences > Profiles > 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.

21
  • 3
    Useful info about the key mappings - I've used these with the (commercial) BetterTouchTool, see this answer.
    – RichVel
    Commented Sep 19, 2017 at 8:43
  • 5
    This doesn't work in most applications in my experience. It is better to use Karabina-Elements.
    – Timmmm
    Commented Oct 25, 2017 at 8:56
  • 8
    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. Commented Nov 16, 2017 at 15:14
  • 7
    This setting works for me on Mac OS Mojave with external keyboard connected.
    – jdhao
    Commented Oct 14, 2018 at 14:56
  • 12
    Unfortunately my Home and End keys still don't behave properly in Slack, which is driving me nuts. But generally this tip is helpful. Many other articles (including damieng.com/blog/2015/04/24/…) mention ctrl-shift-home and ctrl-shift-end, but I've never seen those work, regardless of app.
    – Ryan
    Commented Jan 7, 2021 at 14:54
158

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

13
  • 14
    Karabiner is great and also open source
    – Jedidja
    Commented Feb 4, 2015 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
    Commented Nov 10, 2015 at 15:36
  • 7
    @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
    Commented Jun 19, 2017 at 18:09
  • 30
    If you are in 2020 and like me you have no idea how to setup Karabiner, go to Complex modifications tab > Add rule > Import more rules from the Internet, and then search for "home" and you will find some rules to import. Commented Apr 17, 2020 at 3:29
  • 7
    If you are in 2021 and you want the For PC Rules, go to Complex modifications tab > Add rule > Import more rules from the Internet, and then search for "PC" and you will find some rules to import. (Thanks @RodrigoSaling) for the initial tip
    – JJS
    Commented Jan 20, 2021 at 13:33
57

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.

0
22

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 (legacy) / BetterTouchTool (current) 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.

5
  • 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
    Commented Sep 19, 2017 at 8:41
  • For selecting while jumping (e.g. in word docs or onenote) I you need to also map shift-home to shift-command-left-arrow (note arrow, not control A) and similarly for end. Got it from here community.folivora.ai/t/…
    – Rhubarb
    Commented Mar 20, 2020 at 9:55
  • Finally! I have a solution for this missing mapping. Thanks for the tip.
    – jerik
    Commented Nov 26, 2020 at 7:49
  • I've since switched to Karabiner Elements as explained in Jason Waters' answer. It's easier to set up.
    – mamacdon
    Commented Nov 27, 2020 at 16:18
  • 1
    This solution works great! Only difference is that the UI looks a bit different in 2022 and I had to do make the assigned actions be ⌘ <- and ⌘ ->.
    – pir
    Commented Mar 2, 2022 at 14:33
11

I accomplished this using Karabiner Elements.

You need to update the rules section of ~/.config/karabiner/karabiner.json with the JSON array from code below. Doing so remaps the home and end keys to work the way they work on pc/windows.

{
  "rules": [
    {
      "description": "change home key to command + left_arrow",
      "manipulators": [
        {
          "from": {
            "key_code": "home"
          },
          "to": [
            {
              "key_code": "left_arrow",
              "modifiers": [
                "left_command"
              ]
            }
          ],
          "type": "basic"
        }
      ]
    },
    {
      "description": "change home key to command + left_arrow while presing shift (selection)",
      "manipulators": [
        {
          "from": {
            "key_code": "home",
            "modifiers": {
              "mandatory": [
                "left_shift"
              ]
            }
          },
          "to": [
            {
              "key_code": "left_arrow",
              "modifiers": [
                "left_command",
                "left_shift"
              ]
            }
          ],
          "type": "basic"
        }
      ]
    },
    {
      "description": "change end key to command + right_arrow",
      "manipulators": [
        {
          "from": {
            "key_code": "end"
          },
          "to": [
            {
              "key_code": "right_arrow",
              "modifiers": [
                "left_command"
              ]
            }
          ],
          "type": "basic"
        }
      ]
    },
    {
      "description": "change end key to command + right_arrow while presing shift (selection)",
      "manipulators": [
        {
          "from": {
            "key_code": "end",
            "modifiers": {
              "mandatory": [
                "left_shift"
              ]
            }
          },
          "to": [
            {
              "key_code": "right_arrow",
              "modifiers": [
                "left_command",
                "left_shift"
              ]
            }
          ],
          "type": "basic"
        }
      ]
    }
  ]
}

see also: github gist

enter image description here

2
  • You can simplify this by using "modifiers": { "optional": [ "any"] } to avoid duplicating a rule for selection (this formatting in comment is horrible, copy paste to editor to read properly I s'ppose) { "description": "Home is Beginning of line", "type": "basic", "from": { "key_code": "home", "modifiers": { "optional": [ "any"] } }, "to": [{ "key_code": "left_arrow", "modifiers": [ "left_command" ] }] }
    – n-smits
    Commented Sep 14, 2021 at 10:07
  • 2
    A rule to achieve this can also be imported from the repository, by going into the Complex modifications section and clicking on Import more rules from the internet, that will open a browser at ke-complex-modifications.pqrs.org, search for Home and grab Home and End from the results. Tested on Big Sur. Commented Jan 7, 2022 at 17:08
10

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>
2
  • Has not been updated to work with Sierra, per the link above. Commented May 18, 2017 at 14:35
  • 1
    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
    Commented Sep 19, 2017 at 8:40
9

@Lri's answer is awesome (thank you so much; this was driving me insane), but I ended up modifying it a little.

(In this answer, I use the non-Apple names for keys, so Command = Super and Option = Alt)

SuperLeft and SuperRight used to actually be mapped to moveToLeftEndOfLine and moveToRightEndOfLine. Using moveToBeginningOfLine and moveToEndOfLine, like @Lri does, may cause inconsistent behaviour.

There are whole bunch of other differences between the Apple shortcuts and literally everybody else's shortcuts that I added. For example, ControlLeft and ControlRight are supposed to move between words; not AltLeft and AltLeft. I don't think this is exhaustive, but it's working pretty well for me right now. I assumed the user has already swapped the Control and Super keys to fix cutting, copying, and pasting.

~/Library/KeyBindings/DefaultKeyBinding.dict:
{
    "\UF729"   = moveToLeftEndOfLine:; // home
    "\UF72B"   = moveToRightEndOfLine:; // end
    "$\UF729"  = moveToBeginningOfLineAndModifySelection:; // shift-home
    "$\UF72B"  = moveToEndOfLineAndModifySelection:; // shift-end

    "@\UF700"  = moveUp:;  //super-up
    "@\UF701"  = moveDown:;  //super-down
    "@\UF702"  = moveWordLeft:;  //super-left
    "@\UF703"  = moveWordRight:;  //super-right

    "@$\UF700" = moveUpAndModifySelection:;  // super-shift-up
    "@$\UF701" = moveDownAndModifySelection:;  // super-shift-down
    "@$\UF702" = moveWordLeftAndModifySelection:;  // super-shift-left
    "@$\UF703" = moveWordRightAndModifySelection:;  // super-shift-right

    "@\UF728"  = deleteWordForward:;  // super-delete
    "@\U7F"    = deleteWordBackward:;  // super-backspace
}
8

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.

6
  • iTerm2 also has a "Natural text editing" key mapping preset that includes this behavior.
    – Matt M.
    Commented May 1, 2017 at 20:37
  • Needs to be changed to bindkey "^[[~4" end-of-line. Note the position of the 4.
    – Ty.
    Commented Jun 15, 2017 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~" Commented Jun 15, 2017 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.
    Commented Jun 15, 2017 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? Commented Jun 16, 2017 at 2:12
8
  1. Open this file
/Library/KeyBindings/DefaultKeyBinding.dict (apply to all users)

or

~/Library/KeyBindings/DefaultKeyBinding.dict (apply to yourself)
  1. Add these lines:
{
  "\UF729"  = moveToBeginningOfParagraph:; // home
  "\UF72B"  = moveToEndOfParagraph:; // end
  "$\UF729" = moveToBeginningOfParagraphAndModifySelection:; // shift-home
  "$\UF72B" = moveToEndOfParagraphAndModifySelection:; // shift-end
  "^\UF729" = moveToBeginningOfDocument:; // ctrl-home
  "^\UF72B" = moveToEndOfDocument:; // ctrl-end
  "^$\UF729" = moveToBeginningOfDocumentAndModifySelection:; // ctrl-shift-home
  "^$\UF72B" = moveToEndOfDocumentAndModifySelection:; // ctrl-shift-end
}
3
  • 1
    This answer seems to be just rehashing what's already explained in other answers but substituting your favorite set of keys without much explanation. Perhaps you could add a little more value by explaining where this differs from other solution(s).
    – Caleb
    Commented May 7, 2020 at 12:04
  • 3
    @Caleb: he adds moveToBeginningOfDocument and moveToEndOfDocument, which I find indispensable. Commented Jun 5, 2021 at 9:58
  • damieng.com/blog/2015/04/24/…
    – felickz
    Commented Apr 1, 2022 at 16:20
7

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.

3

This from iExplain works for Big Sur.

{
    /* Remap Home / End keys */

    /* Home Button*/
    "\UF729" = "moveToBeginningOfLine:"; 

    /* End Button */
    "\UF72B" = "moveToEndOfLine:"; 

    /* Shift + Home Button */
    "$\UF729" = "moveToBeginningOfLineAndModifySelection:"; 

    /* Shift + End Button */
    "$\UF72B" = "moveToEndOfLineAndModifySelection:"; 
    
    /* Ctrl + Home Button */
    "^\UF729" = "moveToBeginningOfDocument:";
 
    /* Ctrl + End Button */
    "^\UF72B" = "moveToEndOfDocument:";
 
    /* Shift + Ctrl + Home Button */
    "$^\UF729" = "moveToBeginningOfDocumentAndModifySelection:";

    /* Shift + Ctrl + End Button*/
    "$^\UF72B" = "moveToEndOfDocumentAndModifySelection:"; 
}
1

The original question is already answered but I have put together the equivalence of missing standard layout keyboard keys (Home, Backspace, Page etc) on a Mac here.

-5

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!

2
  • 4
    Could you expand your answer with instructions on how to use the specified software to solve the original problem?
    – grg
    Commented Jun 11, 2015 at 18:35
  • 3
    Not free in either the "Free Beer" or "Freedom" senses. 45-day trial, closed-source, paid software.
    – Wyatt Ward
    Commented Aug 5, 2017 at 5:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .