a paid nerd
a paid nerd

Reputation: 31512

How do I tell Zsh to write the current shell's history to my history file?

I work in a place that has gazillions of tools which require tons of options, so I rely on my shell's history significantly. I even back it up every now and then just to make sure I don't lose useful, lengthy commands.

I just typed one of these commands and I want to make sure it's flushed to the history file, but I have a long-running job in the background and I can't type exec zsh. Is there something else I can do in this situation?

(Sure, I could copy and paste it into a file, but it would be more logical for there to exist a flush-history command.)

Upvotes: 33

Views: 17629

Answers (4)

Mukesh Singh
Mukesh Singh

Reputation: 51

appen below line to ~/.zshrc, it will save 1000 entry we increase by changing value of HISTSIZE and SAVEHIST

HISTSIZE=1000
if (( ! EUID )); then
  HISTFILE=~/.zsh_history_root
else
  HISTFILE=~/.zsh_history
fi
SAVEHIST=1000

Upvotes: 2

PonyEars
PonyEars

Reputation: 2254

And use

fc -R

to read in the history (after writing it) in an existing zsh shell.

Upvotes: 20

a paid nerd
a paid nerd

Reputation: 31512

I also just found:

setopt INC_APPEND_HISTORY

From man zshoptions:

INC_APPEND_HISTORY
   This  options  works like APPEND_HISTORY except that new history
   lines are added to the $HISTFILE incrementally (as soon as  they
   are  entered),  rather  than waiting until the shell exits.  The
   file will still be periodically re-written to trim it  when  the
   number  of  lines grows 20% beyond the value specified by $SAVE-
   HIST (see also the HIST_SAVE_BY_COPY option).

Upvotes: 27

pts
pts

Reputation: 87241

To write the shell history to the history file, do

fc -W

fc has some useful flags, see them all in man zshbuiltins.

You can also fully automate reading and writing the history file after each command (thus sharing your history file automatically with each running zsh) by saying setopt -o sharehistory. Read more history-related options in man zshoptions.

Upvotes: 54

Related Questions