ZSH history

In fact, the shell is able to read and write history without being told. You need to tell it where to save the history, however, and for that you have to set the parameter 

$HISTFILE

 to the name of the file you want to use (a common choice is `

~/.history

‘). Next, you need to set the parameter 

$SAVEHIST

 to the number of lines of your history you want saved. When these two are set, the shell will read 

$HISTSIZE

 lines from 

$HISTFILE

 at the start of an interactive session, and save the last 

$SAVEHIST

 lines you executed at the end of the session. For it to read or write in the middle, you will either need to set one of the options described below (

INC_APPEND_HISTORY

 and 

SHARE_HISTORY

), or use the 

fc

 command: 

fc -R

 and 

fc -W

read and write the history respectively, while 

fc -A

 appends it to the the file (although pruning it if it’s longer than 

$SAVEHIST

); 

fc -WI

 and 

fc -AI

 are similar, but the 

I

 means only write out events since the last time history was written.

There is a third parameter 

$HISTSIZE

, which determines the number of lines the shell will keep within one session; except for special reasons which I won’t talk about, you should set

$SAVEHIST

 to be no more than 

$HISTSIZE

, though it can be less. The default value for 

$HISTSIZE

 is 30, which is a bit stingy for the memory and disk space of today’s computers; zsh users often use anything up to 1000. So a simple set of parameters to set in 

.zshrc

 is

  HISTSIZE=1000
  SAVEHIST=1000
  HISTFILE=~/.history

and that is enough to get things working. Note that you must set 

$SAVEHIST

 and 

$HISTFILE

 for automatic reading and writing of history lines to work.