56

Currently I have zsh set up in such a way that command history is shared between all sessions immediately.

Say I have a terminal emulator open with two tabs, each with a zsh session, A1 and A2. If I enter ls -la in A1, and then go to A2 and press up arrow key, I will see ls -la in the command prompt.

I would like to change it so sessions don't share the command history with each other although when you start new session it gets all the previous history from all sessions before it.

3
  • I do that regularly. There's however a lot of questions which still have no satisfactory answer.
    – Art
    Commented Apr 15, 2012 at 23:21
  • Great question and helpful answers. I thought this was an iTerm2 thing specifically. Hopefully this comment will someone this way if they make the same assumption.
    – Alan H.
    Commented Sep 6, 2017 at 22:59
  • @Art congrats for the first k. Maybe one could add oh-my-zsh as question tag besides zsh and history..
    – Timo
    Commented Oct 11, 2020 at 10:28

3 Answers 3

59

You've probably got INC_APPEND_HISTORY set.

The INC_APPEND_HISTORY option, from man zshoptions:

This options works like APPEND_HISTORY except that new history lines are added to the $HISTFILE incrementally (as soon as they are entered), rather than waiting until the shell exits.

The option that you want is APPEND_HISTORY:

APPEND_HISTORY If this is set, zsh sessions will append their history list to the history file, rather than replace it. Thus, multiple parallel zsh sessions will all have the new entries from their history lists added to the history file, in the order that they exit. The file will still be periodically re-written to trim it when the number of lines grows 20% beyond the value specified by $SAVEHIST (see also the HIST SAVE BY COPY option).

You can read about these options in the man zshoptions, man zshall or online here.

To set them, in your ~/.zshrc or similar, you should have:

setopt APPEND_HISTORY

Be aware that, if you're using oh-my-zsh by default, I believe, INC_APPEND_HISTORY is used. I'm not 100% sure which way around things get loaded, but if the oh-my-zsh option overrides the one you've set in ~/.zshrc, you can fiddle with it in ~/.oh-my-zsh/lib/history.zsh

3
  • 1
    echo unsetopt INC_APPEND_HISTORY >~/.oh-my-zsh/custom/history.zsh
    – youfu
    Commented Feb 28, 2016 at 12:00
  • How can iTerm2 be configured to restore the individual history of tabs after a restart (for zsh and oh-my-zsh)?
    – Googol
    Commented Feb 22, 2019 at 6:57
  • I quite like the shared history, but every so often I want to see the history of a single session. Is there a way to do that?
    – RoG
    Commented Feb 14, 2022 at 12:10
19

To add to the accepted answer, if you use oh-my-zsh, you probably have SHARE_HISTORY option enabled by default.

To achieve separation between running shells + entire history combined in a newly opened terminal, you have to comment or remove the following line:

setopt share_history # share command history data

from ~/.oh-my-zsh/lib/history.zsh

3
  • In my case, the path of history.zsh file is ~/.antigen/repos/https-COLON--SLASH--SLASH-github.com-SLASH-robbyrussell-SLASH-oh-my-zsh.git/lib/history.zsh Commented Aug 19, 2016 at 16:25
  • 11
    Another way to go about it is to put unsetopt share_history in your .zshrc after oh-my-zsh is loaded. Documented here. Commented Jan 5, 2017 at 10:15
  • thanks @counterbeing that fixed it for me -- you should make that a separate answer, it's the easiest way to fix it
    – jcollum
    Commented Dec 13, 2017 at 16:49
0

If you are using oh-my-zsh, add:

unsetopt share_history

On your .zshrc, after oh-my-zsh is loaded (source $ZSH/oh-my-zsh.sh).

Source. Credits.

You must log in to answer this question.

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