9

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 ?

1
  • 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

10

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.

4
  • 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
  • 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
3

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"
0

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.

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.