How does Zsh keep track of command history?
I see two files in my home directory: .zhistory
and .zsh_history
. What is the difference between them? The files seem to contain all of my history. Does Zsh keep appending lines to one of them forever? Wouldn't that make
history search too slow?
Can I safely make these files symbolic links to another location? (e.g. a git repository to keep track of changes to the history)
Best Answer
Zsh stores input lines (possibly with time information) in the file indicated by the variable
HISTFILE
. This should be an absolute file name (otherwise it will be interpreted relative to whatever directory is current at the time).Zsh has no built-in default value for
HISTFILE
. The zsh distribution comes with a setup wizard for new users which has the value~/.histfile
built in, so this is what zsh users get by default. Oh-my-zsh comes withHISTFILE=.zhistory
preset. So it looks like you tried zsh both with the default setup and with oh-my-zsh. Check your.zshrc
(or the file dates) to see which one you're currently using.How zsh opens the file, and therefore what happens if it is a symbolic link, depends on several options.
append_history
(set by default),inc_append_history
orshare_history
is set, or when the history is saved explicitly withfc -AI
, zsh appends to the existing file.Note that even under these settings, zsh occasionally overwrite the file as described below to trim it down to size.
hist_save_by_copy
is set (it's set by default since zsh 5.0, but does not exist in 4.2), zsh writes a temporary file then moves it in place when complete. In this case, if the history file was a symbolic link, the new file replaces the symbolic link.Rather than point zsh to a symbolic link, set
HISTFILE
to wherever you want the history file to be.How many lines of history are kept is configured through the variables
HISTSIZE
andSAVEHIST
.HISTSIZE
is the maximum number of lines that are kept in a session andSAVEHIST
is the maximum number of lines that are kept in the history file.To get some history saved at all, you need to set both
HISTFILE
andSAVEHIST
, as the default value ofSAVEHIST
is 0. You may want to increaseHISTSIZE
as well (as of zsh 5.0, the default is only 30).