Preserve tcsh history in multiple terminal windows

These variables set the history to merge itself instead of overwrite, and not save duplicates:

set history=1000
set histdup=erase
set savehist=(1000 merge)

the secret sauce is this line:

alias precmd 'history -S; history -M'

which will save and merge your history prior to printing the prompt - i.e. after each command you type.

all of the above should be added to your .tcshrc file.


In addition to Idan answer, I want to add that alias precmd 'history -S; history -M' potentially can mess up the history file, since it also records SIGINT and EOF signal (Ctrl+C and Ctrl+D).

enter image description here

A better way would be save and merge current shell history after running a command.

You can achieve it by this setup below instead, since the tcsh doesn't recognize SIGINT or EOF as a valid command.

alias postcmd       "history -S; history -M"