1

I've recently switched from bash to zsh shell. In contrast with bash, when I re-login the history is empty. When I press up/down nothing displays. How to set zsh to save the history between sessions?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

1

Zsh has a minimalistic configuration out of the box which doesn't include nice features such as saving the command history between sessions. This and other features are available, but turned off by default.

Depending on how your operating system is set up, it may present you a configuration screen (zsh-newuser-install) the first time. If it doesn't, or to run it again, see Run the Zsh first use wizard

In the first-time configuration screen, select (1) Configure settings for history, change the settings if you want, then (0) Remember edits and return to main menu. While you're at it, activate command-sensitive completion with (2) Configure the new completion system. and either (1) Turn on completion with the default options. or (2) Run the configuration tool (compinstall). Also activate some basic features in (4) Pick some of the more common shell options: I recommend turning on autocd, extendedglob. Finally select (0) Exit, saving the new settings..

If you prefer doing the configuration manually, you need to set both HISTFILE and SAVEHIST and you'll probably want to increase HISTSIZE too. These settings go into the file .zshrc in your home directory.

# Save history between sessions
HISTSIZE=10000
SAVEHIST=$HISTSIZE
HISTFILE=~/.zsh_history

# Turn on some useful options
setopt auto_cd
setopt extended_glob

# Activate context-sensitive completion
autoload -Uz compinit
compinit

Here are some more basic settings that the first-time configuration screen doesn't cover.

  • Prevent against accidentally overwriting a file with output redirection or with mv or cp.
  • Make the mass-renaming function zmv available.
  • The default prompt is bland and uninformative by default. To get you started, here's a prompt that shows the last 3 components of the current directory and the status of the previous command if it failed.
# Prevent against accidentally overwriting files
setopt no_clobber
alias cp='cp -i'
alias mv='mv -i'

# Rename, copy or link files based on patterns.
autoload -U zmv
alias zcp='noglob zmv -C'
alias zln='noglob zmv -L'
alias zmv='noglob zmv'

# Prompt
PS1='%B%(?,,[%?] )%3~%# %b'