Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Why does fn-- (or others) do a home/end in Terminal but a - works everywhere else?

So just to be clear the question is how to make Terminal take Command + and for home and end?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

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.

(Surprinsingly, Terminal provides Option and Option which behaves as you would expect, moving the cursor to the beginning of the previous/next word. That only works reliably in bash, in vi, for example, only the latter works.)

You can still configure and to match CtrlA and CtrlE so that it works in bash and other shells:

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

  2. Press +, select "cursor left", "shift" as modifier and action "send string to shell". Then press CtrlA (\001 will appear in the text field`) and OK: enter image description here

  3. Now add a key combination for "cursor right" and press CtrlE.

  4. Open Automator (for example, typing space to open Spotlight and clicking 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}
            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}
            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

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 OS X 10.3.3 Yosemite)

share|improve this answer
1  
Thanks for the detailed answer, is there a petition to just have Apple include a home/end button on the next redesign :-) –  Jackie Nov 12 '12 at 15:09
    
I'm glad I could help. –  jaume Nov 12 '12 at 15:22

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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