How to stop ZSH from merging history for all closing tabs?
Asked Answered
D

3

20

I use combination of zsh with oh-my-zsh and iTerm2 for development on Mac. I'm frustrated with the following issue:

  • N tabs opened
  • close the terminal
  • reopen it (with Use system Window Restoration Setting)
  • the history from all previously opened tabs got merged into one for every reopened tab

The question: How to preserve separated history for every reopened tab?

Delia answered 19/2, 2018 at 20:24 Comment(0)
M
28

Per https://github.com/robbyrussell/oh-my-zsh/issues/2537,

Add unsetopt share_history to your .zshrc file.

Mousetail answered 19/2, 2018 at 20:33 Comment(3)
It seems like disabling shared history for running tabs. But is still got merged after restore.Delia
any idea what the .bash_profile equivalent of this should be?Springy
This pleases Zulthar.Autogamy
M
4

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.

Moua answered 20/2, 2018 at 7:55 Comment(5)
So, looks like there's no easy way?Delia
i have set up like this but doesnt work: source $ZSH/oh-my-zsh.sh setopt noincappendhistory unsetopt nosharehistory unsetopt appendhistoryCinquecento
@Cinquecento This is equivalent to setopt sharehistory noincappendhistory noappendhistory, which would be a valid setting. Aside form that HISTFILE needs to be set to a filename in an existing directory and SAVEHIST and HISTSIZE need to be set to positive integer values. If either HISTFILE or SAVEHIST are unset, no file will be written or read. If HISTSIZE is unset or 0, the history will be written, but will not availabe in the shell.Moua
@Moua i dont get so what should be written in zshrc to make it work?Cinquecento
@Arie: I don't think you can make it work without either (1) changes to how iTerm "saves" sessions, or (2) having a different history file for each shell session.Bastille
E
0

Alternative solution using iTerm2 - storing tab-specific history to a separate file, surviving restart.

One time setup

mkdir ~/.zsh_history_tabs

Edit ~/.zshrc

unsetopt share_history
unsetopt inc_append_history

HISTFILE_BASE=~/.zsh_history_tabs/iterm_
HISTFILE_TEMP=~/.zsh_history_tabs/tmp_$$
HISTFILE_TERM=~/.zsh_history_tabs/iterm_$ITERM_SESSION_ID
HISTFILE_GLOB=~/.zsh_history
 
# Generate a unique but persistent file name based on iTerm session ID
if [[ -n "$ITERM_SESSION_ID" ]]; then
    export HISTFILE=$HISTFILE_TERM
    
    # For new terminals, create histfile by combination of all tabs
    if [[ ! -f "$HISTFILE" && -f "$HISTFILE_GLOB" ]]; then
        cp "$HISTFILE_GLOB" "$HISTFILE_TEMP"
        tail -n30000 ${HISTFILE_BASE}* >> "$HISTFILE_TEMP" 2>/dev/null
        mv $HISTFILE_TEMP $HISTFILE
    fi
else
    export HISTFILE=$HISTFILE_GLOB
fi

Save current history (migration)

In each open tab:

history >> ~/.zsh_history_tabs/iterm_$ITERM_SESSION_ID
Erikaerikson answered 3/5 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.