18

I switched to ZSH from BASH as per the upgrade in Catalina and all is well except my home/end keys no longer work. This can be really annoying trying to move my cursor over super far to the left instead of a single home click.

Any idea if they just changed the key you use to "go to start of command" instead of home? Or is there a bug?

2
  • 4
    Note that Ctrl-A and Ctrl-E does this (emacs bindings). Commented Jul 15, 2020 at 22:36
  • Sorry for the silly question, but what is the "home" key on a Mac? Neither my MacBook nor my iMac have a key labeleld "Home" or "End". In zsh, I always use Control-A and Control-E, though as far I see, you can do more fancy keybindings if you use i.e. iTerm2 as a terminal. Commented Aug 14 at 8:19

5 Answers 5

35

The other answers didn't work for me when connecting over SSH to the Mac. I had to add the following lines to my ~/.zshrc to get Home and End to work:

bindkey "^[[H" beginning-of-line
bindkey "^[[F" end-of-line
1
  • 1
    It also works in Linux. Thanks! Commented Mar 16, 2023 at 10:55
11

@jemcclin's answer did not work for me. Here's what worked for me on latest macOS Catalina 10.15.5

  1. Open Terminal -> Preferences...

  2. Select your preferred profile

  3. On the Keyboard tab, add a new binding (or modify an existing one)

  4. Use the following settings to make Home work again:

    • Key: Home (↖)
    • Modifier: None
    • Action: Send Text
    • Text to send is \001 (CTRL+A)
  5. Use the following settings to fix the End key:

    • Key: End (↗)
    • Modifier: None
    • Action: Send Text
    • Text to send is \005 (CTRL+E)

Extra: btw, i was having the same Home/"Scroll to top" issues on webpage forms (like the one i'm typing right now to answer this) and what fixed for me was this

https://www.iexplain.org/remap-home-and-end-buttons-for-external-keyboard-on-mac/

7

It seems the control characters required for zsh in Terminal are slightly different than the ones that worked in bash.

To fix your Home/End keys:

  1. Open Terminal -> Preferences...
  2. Select your preferred profile
  3. On the Keyboard tab, add a new binding (or modify an existing one)
  4. Use the following settings to make Home work again:
    • Key: Home (↖)
    • Modifier: None
    • Action: Send Text
    • Text to send is \033[1~ (For bash, \033OH was the value that worked)
  5. Use the following settings to fix the End key:
    • Key: End (↗)
    • Modifier: None
    • Action: Send Text
    • Text to send is \033[4~ (For bash, \033OF was value that worked)

This fixes my Home/End keys in zsh, and also works correctly when connected over SSH to terminals running bash.

Note that if you have keybindings specified in your ~/.zshrc or are running a third-party software to adjust your keybindings globally (eg: Karabiner), you might need to check or undo any settings in those places that may affect the Home/End keys if you're getting weird behaviour.

4

@jemcclin's answer requires the following entries in ~/.zshrc to work

bindkey "\e[1~" beginning-of-line
bindkey "\e[4~" end-of-line
2
  • Thanks! If you ever want to edit another answer to clarify, that’s encouraged as well as posting an answer.
    – bmike
    Commented Apr 9, 2020 at 17:38
  • 1
    Thanks Mike. I wasn't aware that answer-editing was a thing for novice users. But I'll consider that option if the need arises again. (I went for a 2nd answer, because my reputation was too low to add a simple comment)
    – frame
    Commented Apr 10, 2020 at 13:01
0

If you happen to be using Alacritty as your terminal, the following can be added to your user's configuration file (e.g., ~/.config/alacritty/alacritty.toml):

[[keyboard.bindings]]
chars = "\u0001"
key = "Home"

[[keyboard.bindings]]
chars = "\u0005"
key = "End"

This thread seems to be confused about where to define key-bindings, and thus are surprised when key-bindings work over SSH (or not).

Conceptually, you can define key-bindings in your interactive shell (e.g., ~/.zshrc, ~/.bashrc), or a system-wide line editing library like GNU Readline (~/.inputrc), or even input monitoring tools like X11's bindkeys (/etc/xbindkeysrc). These will only affect key presses on that single system/shell/library session.

Alternatively, since you typically have a 1:1 correspondence between physical keyboard and terminal software, it makes more sense to define the key-bindings through your terminal's configuration. This way, regardless of which system you happen to be running a shell, the common middleman is your terminal, and the key-bindings will remain effective.

Also, while I believe this is true, I haven't confirmed that: in almost all scenarios your terminal software is going to receive key-press events earlier than any interactive shell/line editor. It makes sense in my mind to handle the key-bindings as early as possible.

You must log in to answer this question.

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