Unless iTerm2 actually just hides a tab on closing and keeps the shell session running in the background (which, according to the iTerm2 website, seems to be an option), it is not possible to completely restore a shell session. In your case it seems like a new shell session is created when restoring a tab, which leads to the history being read from $HISTFILE
.
If you want to prevent any merging of history, you have to ensure that of the options APPEND_HISTORY
, INC_APPEND_HISTORY
and SHARE_HISTORY
only the first one is set:
setopt noincappendhistory
setopt nosharehistory
setopt appendhistory
This will lead to new entries in the history (i.e. commands run during the session) only being appended to the history file when the shell exits. So when you close a session, the next shell (re-)opened will have lines of the just previously closed shell on the bottom of the history.
Another option could be to have separate history files for each shell session and device your own method of loading a history from these files using the fc
builtin. This would at least in part depend on whether it is possible to differentiate between iTerm2 tabs from within a shell session (for example via some environment variable) and whether this holds true when re-opening a tab.