1

I am using the following code in my .bashrc file for keeping everything in history when I have multiple tmux panes open:

# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend

# After each command, append to the history file and reread it
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

I have no problem with this on my linux machine. However on macOS, there is a little problem when using this with tmux specifically (meaning multiple opened asynchronous terminals without tmux work okay).

What then happens is that a new pane in tmux has completely empty history.

Here is a reference comment in question Preserve bash history in multiple terminal windows, showing the same problem on macOS with similar program screen:

comment reference

I tried to remove the history -c; part of the command and now the history in new tmux pane is not empty. However, I am not sure whether it is a working solution and what caveats it might have. Therefore, my main question is what role does the history -c play and why is it needed.

Note: If you are unable to reproduce the problem, point it out and I will provide os versions, program versions and logs.

1 Answer 1

0

The simple, short answer is that, if you do not do an history -c before a history -r you would read the history twice.

The last command (and its number) is given with fc -l -1:

$ fc -l -1

19131    clear

If you read the history file again, you will duplicate most of the history (in memory, not in the file):

$ history -r; fc -l -1

38319    fc -l -1

That is the reason to have and use history -c.

empty history

What then happens is that a new pane in tmux has completely empty history. The workaround for that must be very simple: just execute

history -r

And all the history in the history file will reappear in memory.

You must log in to answer this question.

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