53

Home and End do nothing in iTerm2, nor do ⌘← and ⌘→.

However, Ctrl-A and Ctrl-E do work.

Can Home and End do this?

Preferences->Keys_Key Bindings in iTerm2 do not offer a "Start of Line" or "End of Line option.

Related: 1, 2

1

3 Answers 3

74

^ ControlA and ^ ControlE are keybinding in Bash and ZSH, not in iTerm (or Terminal).

If you want custom bindings within iTerm, you will need to bind the actual keyboard sequence in the keybinding. "Start of Line" and "End of Line" are descriptors, not the actual key strokes.

So, to do this you'd need to send ^ ControlA for Home via their Hex Codes. In this case, the codes would be 0x01 for "start of text" (^A) and 0x05 for "ENQ" (^E).

In the image below, you can add a new binding in Preferences → Keys → Key Bindings

  1. Click on + to add a new binding
  2. Click on Record in the dialog box
  3. Press your desired key (Home in this case).
  4. Select your Action (you can scroll or search). Select "Send Hex Code"
  5. Enter the Hex Code (0x01 for "start of text")
  6. Test it out!

iTerm2 Keybinding Home to ^A

3
  • 2
    Note: I'm trying to add some screen shots, but my Dell 104 key keyboard with the Home/End/PgUp/PgDn etc. buttons seems to have developed legs and a passion for ice cream as it apparently left the house. As soon as I get my keyboard back, I'll get some screen shots going.
    – Allan
    Commented Apr 26, 2020 at 20:12
  • 3
    Thank you! One correction: 0x05 is End, while 0x03 that you gave is Ctl-C (break out of the line.)
    – Joshua Fox
    Commented Apr 27, 2020 at 8:00
  • This answer is better: stackoverflow.com/a/52920414 since it works for VIM as well. Commented Jan 15 at 0:56
61

The accepted solution proposed by Allan is not optimal, as this keybinding affects *ALL* applications that are executed on the terminal and this key remapping would mean that some applications won't work as expected:

For example, if you use 'vi' then the keys 'Home' and 'End' no longer work after this key binding (those keys work ok if you don't do the proposed key binding)

A better solution is to do the key mapping on the shell itself:

In zsh (the default shell being used by macos nowadays), just add this in ~/.zshrc:

bindkey '\e[H'    beginning-of-line
bindkey '\e[F'    end-of-line

and Home and End keys will work ok in both the command line and vi

7
  • 8
    This is the optimal solution!
    – dpwrussell
    Commented Aug 29, 2021 at 17:26
  • 1
    While you said about remaping for all apps is true the bindings you have provided does not work. Commented Mar 9, 2022 at 17:10
  • 4
    This should be the accepted solution!
    – Claudiu
    Commented Apr 12, 2022 at 20:48
  • 1
    Works great. Thanks!
    – liberborn
    Commented Jun 23, 2023 at 14:16
  • Shouldn't it be \e[D for beginning of line? Atleast in my iTerm, pressing Option and CursorLeft seem to produce escape-D Commented Aug 16, 2023 at 9:23
6

For me the best answer was a combination of the above:

  1. Preferences->Keys: Remove the default key bindings in iterm for Home and End. This will cause Home (fn+left arrow in mac) & End (fn+right arrow in mac) to work normally as in linux and not as Scroll to Top or Scroll to Bottom respectively.
  2. Preferences->Profiles->Keys: Make sure that the box for "Page up, page down, home and end..." is not checked.

This allows to jump to start or end of line in all cases using Home & End keys, including in 'vi'

2
  • I may be missing something, but this doesn't quite work. I don't have "the default key bindings" you mention, ie. they're already "removed". This was the answer: stackoverflow.com/a/52920414 that works at the CLI and in Vim. Commented Jan 15 at 0:56
  • The option to uncheck was important in my scenario, together with the bindkey solution above Commented Apr 5 at 0:47

You must log in to answer this question.

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