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, 2017 at 21:05

3 Answers 3


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? Mar 14, 2017 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, 2017 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. Jan 1, 2018 at 9:53
  • 1
    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 Aug 11, 2020 at 0:08
  • @PeterGluck I too have the problem of tcsh history numbering being messed up (I'm at >30,000) regardless of whether I use this method or the one you referenced. Here's an archived URL for the above: web.archive.org/web/20221001183120/http://hints.macworld.com/… Jul 10 at 22:56

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"
  • I think this is a better answer, and should have been the accepted one. Oct 20, 2022 at 7:57

A single global history file is generally not what is desired, as it garbles command sequences when working on several tabs simultaneously.

If you want to keep your history PER TAB, use this approach:

set histfile=~/.history_`tty|sed "s#^.*/##"`
set history=1000
set savehist=(${history} merge)
alias postcmd 'history -S'

This will create individual history files for each tab, depending on their tty ID.

Using xfce4-terminal, if you do not change the order of tabs, and don't close tabs except the rightmost ones, this will correctly restore each history to its associated tab (and therefore, path). As re-ordering tabs takes the tty ID with them, but a xfwm4 session restore will re-create each tab from scratch (assigning new tty IDs starting from 0), it would restore the history files to the "wrong" tab (i.e., not the one with the directory path of their original tab). I haven't found a solution yet how to keep track of these changes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .