This can be set in Terminal.app preferences
Context
To answer the one about how to get the beginning or end of the line, it appears that by default Terminal maps these keys to it:
- shift+home → beginning of line, equivalent to the "home" key in normal terminals
- shift+end → end of line, equivalent to the "end" key in normal terminals
Instructions
If you want home and end to work the "normal" way (and not require shift), go to the Keyboard tab of the Terminal profile preferences:
[Terminal menu] → Preferences → Profiles tab (or settings on some versions of OS X) → Keyboard sub-tab
Then modify/add these keys to be the following "send string to shell". Use the code for your shell, Bash or Zsh.
macOS changed the default shell from Bash to Zsh in MacOS Catalina (v10.15) and later versions (BigSur and Monterey). The instructions are different for Bash and Zsh.
Zsh
You can enter these codes in the edit dialog by pressing ctrl-A for /001
and ctrl-E for /005
. Thanks to @JW for the Zsh information.
Another, unrelated, option to map home/end keys for MacOS Catalina (v10.15) and later versions (BigSur and Monterey) is to use bindkey widgets:
sudo echo 'bindkey "\033[H" beginning-of-line; bindkey "\033[F" end-of-line' >> ~/.zshrc
Bash
You can get the \033
part by hitting the escape key within the edit dialog input, if you need to add it.
In later versions of Mac OS X, if the terminal screen shifts up or down when you press the home/end key, the home key may need to be set to \033[1~
and the end key to \033[4~
to get the results you want (no shift needed).
Conclusion
Then home and end will work like normal again (phew).
Also note that "alt + ←" and "alt + →" by default in terminal map to word left and word right, another handy combo to remember.
Feel free to modify this answer to add more useful key bindings, as it is a community wiki.