51

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)

68

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 with HISTFILE=.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.

  • If one of the options append_history (set by default), inc_append_history or share_history is set, or when the history is saved explicitly with fc -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.
  • Otherwise, if the option 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.
  • Otherwise zsh overwrites the existing file in place.

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 and SAVEHIST. HISTSIZE is the maximum number of lines that are kept in a session and SAVEHIST 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 and SAVEHIST, as the default value of SAVEHIST is 0. You may want to increase HISTSIZE as well (as of zsh 5.0, the default is only 30).

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.