How can we preserve or maintain the same history across multiple terminals?

The same question, but for bash shell , were discussed in the below link

Preserve bash history in multiple terminal windows

let me know the corresponding settings for tcsh shell ?

  • For more information on tcsh's history, see man tcsh. Searching for history -S should jump you to the most relevant part. – Randall Dec 28 '17 at 21:05

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.

  • When I added this alias precmd 'history -S; history -M' nothing occurred when I wrote any command. Is there something else that should be done? – Ayman Salah Mar 14 '17 at 11:53
  • @AymanSalah what occurs is not obvious - it's the .history file (or histfile shell variable value, if set) getting written to disk. Without the precmd alias set, ls -l .history will show the .history file as a untouched. With precmd set as above, ls -l .history will show the updated timestamp and size, as it gets written with each command. – Randall Dec 28 '17 at 20:21
  • @Randall Thank you for pointing that out. I had problems then with the shell session. What you're saying is 100% correct. – Ayman Salah Jan 1 '18 at 9:53
  • This answer made my command number jump in large increments (sometimes thousands). The solution here worked for me: hints.macworld.com/article.php?story=20070715091413640 – Peter Gluck Aug 11 '20 at 0:08

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"

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.