How to get fn + left key working on macOS after changing to zsh from bash?

In case it is not clear fn + left key is simply the HOME button. It takes you to beginning of line. This seems to still work when editing a file (in vim) say so I think zsh does something with the bindings. fn + left now results in an "error" sound.

UPDATE:

posting this link here as it seems where to go for this kind of general stuff: What are the practical differences between Bash and Zsh?

Changing from bash to zsh costs everyone a lot of time and money. Apple has decided to push this on everyone for various reasons. There should be good defaults for everything. Home keys should work, PS1 should be reasonable. Old .bashrc and .profile translations should be automatically generated. Not doing this is stealing from users. We are bailing them out for their "bad" decision to use bash or equivalents bash's decision to change license.

How to make everything "just work" like for bash?

I use iTerm 2 like a normal person. Not sure if that is related.


Solution 1:

To get fn-left (Home) and fn-right (End) working, add these two lines to ~/.zshrc (for your user only), or /etc/zshrc (for everybody)

bindkey "^[[H" beginning-of-line
bindkey "^[[F" end-of-line

Solution 2:

Bash is included in macOS 10.15 Catalina still however it is not the default login shell for new accounts.

This was done as Apple has increasingly been removing dependencies from the core OS for security reasons.

In this particular case since version 4 bash switched from the GPLv2 to the GPLv3 license which is not compatible with how Apple distribution their OS. This has prevented Apple from distributing updates beyond version 3.2 of bash.

Zsh was selected by Apple specifically for compatibility reasons and because it's license is compatible with Apple's distribution method.

The good news is you can switch back to bash 3.2, there are two ways to access it:

  1. Type bash into your terminal application.

  2. Modify the default (login) shell in iTerm or Terminal to bash

How to install bash v5

You can install the latest bash using homebrew:

  1. Install homebrew: https://brew.sh/
  2. Run brew install bash
  3. Switch to bash by running bash or editing the default (login) shell in iTerm or Terminal.

How to change the default (login) shell

  1. Find the path to your desired shell in a terminal application which bash it'll be something like /usr/local/bin/bash
  2. Launch System Preferences
  3. Go to 'Users & Groups'
  4. Right click on your username.
  5. Select 'Advance Options...'
  6. Paste your new shell in the 'Login shell:' field
  7. Press 'OK'
  8. Relaunch your terminal applications.

Solution 3:

The core of the problem is that bash is using the readline library and zsh has its own keybindings and is not using readline. See Make zsh use readline instead of rle.

If you are at ease with the emacs key bindings (this is the one zsh is using by default), then you could use ctrl+a and ctrl+e to get the functions you are looking for within zsh.

If on the other hand, you are at ease with the vi key bindings, then you could enter within your ~/.zshrc:

bindkey -A viins main

and then you would be able to use 0 and $ to get the functions you are looking for within zsh. You can display all these bindings with:

bindkey

If you are interested to continue with zsh, read man zshzle to discover the functions zsh provides at the line editing level.

<ad section>

After many years of use of many ≠ shells, I feel that zsh has better time saving functions than bash.

</ad section>