3

When I log into the Terminal app on OSX my default shell is /bin/bash. Entering echo $HISTFILE points me to /Users/monkeybrain/.bash_history and head $(echo $HISTFILE) shows me that the file contains history entries.

If I then type zsh to switch shell a couple of weird things happen. Firstly even though my prompt changes (so it looks like I'm in the new shell) my $SHELL environment variable still points to /bin/bash. The $HISTFILE variable is now empty, and there is no such file as ~/.zsh_history. Also, I can't use Ctrl-A or Ctrl-E to move through the command line.

What's happening here?

Many thanks,

MB

4
  • The SHELL environment variable is usually not modified by the shell. It usually contains the full path to the current user's login shell, not the shell currently running. In most cases SHELL should be set before a shell starts. zsh does not change the value of SHELL and bash sets it back to the login shell if SHELL was unset.

  • By default HISTFILE is unset in zsh. If you want to save the history, you have to set it to the file you want to use for that:

    HISTFILE="${HOME}/.zsh_history"
    

    You also need to set SAVEHIST to positive integer values to tell zsh how many lines to save in the HISTFILE, the default is 0 (Thanks Monkeybrain for the hint). You probably also want to raise HISTSIZE, which denotes how many lines of history are available in the shell session. Its default is only 30. For example:

    HISTSIZE=2000
    SAVEHIST=2000
    

    This usually is done in the ~/.zshrc configuration file.

  • zsh tries to guess your key binding preferences by checking the EDITOR and VISUAL environment variables. If either contains the string "vi" zsh will use the viins input mode. Neither Ctrl+A nor Ctrl+E are by default bound in this mode. If you want to use the emacs input mode (the default in bash) you just need to run

    bindkey -e
    

    This can also be done in ~/.zshrc

All in all it sounds like you are running zsh with the default configuration. If there is no ~/.zshrc yet, you can just create it and add the necessary settings.

3
  • Wow, thank you!! I've learned lots here. I had no idea that Ctrl+A and Ctrl+E was 'emacs mode' - I just assumed it was standard shell behaviour. Also the textbook I have been using to learn about the shells has misled me regarding how zsh handles history files. Thanks for taking the time to write such a detailed post. – Monkeybrain Apr 15 '16 at 12:03
  • 1
    For the benefit of anyone else having this issue, I should add that in order for .zsh_history to be populated, as well as setting HISTFILE I needed to set SAVEHIST to a non-zero value. – Monkeybrain Apr 15 '16 at 14:29
  • @Monkeybrain Thanks for the hint. I added it to the answer. – Adaephon Apr 18 '16 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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