How do I make ⌘← and ⌘→ work for home/end combo for Terminal?

As convenient as it would be, Terminal can't easily provide the functionality you ask for.

The reason is that programs that run inside Terminal have different rules for moving the cursor to the beginning/end of the line, for example:

  • bash (and other shells) use CtrlA and CtrlE
  • vi uses 0 and $/Shift4

Other programs may use other keys. There isn't a universal key combination for positioning the cursor at the beginning/end of the line.

Of course, Terminal could maintain a list of key combinations for a number of common programs, check what's running and send the correct keys for you, or it could provide a menu item like Move Cursor To Beginning Of Line Inside Shell ⌘← and then send CtrlA behind the scenes. But I guess Apple engineers thought that if you venture in CLI territory you know its idiosyncrasies.

As a side note, Terminal provides Option and Option which behaves in bash as you would expect and moves the cursor to the beginning of the previous/next word. That's because these key combinations are mapped to Escb and Escb (see man bash, section "Commands for Moving" for more information). In vi, for example, only the latter (kind of) works (at the end of this answer I'll show how to configure vi to work with Option and Option).

But thanks to Automator, AppleScript and bash aliases you can get and to work with bash (and possibly other shells) and vi.

The first step is to map and to CtrlA and CtrlE. In a second step, I'll show you how to configure vi to honor CtrlA and CtrlE (which, as a result, will make vi compatible with and ).

Map and to CtrlA and CtrlE

  1. In Terminal, go to Preferences...>Settings>[your active profile]>Keyboard: enter image description here

  2. Press +, select "Cursor Left", "Control-Shift" as modifier and action "Send Text:". Then press CtrlA (\001 will appear in the text field) and confirm with OK: enter image description here

    (In a previous version of this answer I used "Shift" as a modifier. I've noticed that this setting conflicts with shift, which shows the previous Terminal tab, if you don't let go of immediately. Using "Control-Shift" fixes that.)

  3. Now add a key combination for "Cursor Right", but press CtrlE (\005 will appear in the text field). Confirm with OK.

  4. Open Automator (for example, hit space to bring up Spotlight and type Automator), select File>New and choose Service:

    enter image description here

  5. Drag Run AppleScript to the right pane: enter image description here

  6. Type the following script:

    on run {input, parameters}
        tell application "System Events"
            tell application process "Terminal"
                key code 123 using {shift down, control down}
            end tell
        end tell
    end run
    

    and save as Move Cursor to Beginning of Line:

    enter image description here

  7. Repeat steps 4. to 6. with this script:

    on run {input, parameters}
        tell application "System Events"
            tell application process "Terminal"
                key code 124 using {shift down, control down}
            end tell
        end tell
    end run
    

    and save as Move Cursor to End of Line:

    enter image description here

  8. This is what Terminal>Services looks like now:

    enter image description here

Configure vi to honor CtrlA and CtrlE

  1. Open .bashrc with your favorite editor and add this line:

    alias vi='vim -c "map <C-A> 0" -c "map <C-E> $" -c "map <Esc>b B" -c "map <Esc>f E"'

    The first two map commands tell vim to interpret ControlE and ControlA as $ and 0, which move the cursor to the beginning or end of line, respectively.

    The last two map commands tell vim to move the cursor one word at a time as you would expect when pressing option and Option.

Caveats

You will notice that there is a short delay between the moment you press or and the moment the cursor actually moves.

Bear in mind that it will only work for programs that honor CtrlA/CtrlE, so you may still want to learn the specific key combinations of each program you use in Terminal.

(Tested on macOS Sierra 10.12.4)


Of course its possible.

First of all, you can use the free tool "DoubleCommand" if you have a windows keyboard with HOME/END keys to make'em behave like they should (beginning /end of line).

Secondly, for terminal specifically, go to the preferences, Keyboard, edit the icon/key which looks like an arrow pointing up-left, select "send text" and type ctrl+a. Done!

Same for the end button..

If you dont have a PC keyboard, you can do the same with other key combinations.