62

I’m using iTerm2 (version 3) and like it. But something that it has been doing for a while that's really annoying is that the command history is shared between tabs. E.g., in your first tab, run command foo and command bar, then go to your second tab and run say Hello, and then go back to the first tab; I would like and expect the up arrow to suggest bar and then foo, but instead it will suggest say Hello.

Is there a way to correct this behavior?

5
  • 6
    If you are using zsh, append these two lines to .zshrc : unsetopt inc_append_history and unsetopt share_history.
    – harrymc
    Sep 6, 2017 at 6:42
  • @harrymc If you made this a full answer, I could accept & award bounty… I ended up finding superuser.com/a/412439/60337 which is really my answer. I’m an Oh My Zsh user
    – Alan H.
    Sep 6, 2017 at 22:58
  • Done as requested.
    – harrymc
    Sep 7, 2017 at 5:41
  • What about if you're using bash?
    – Ben Wilson
    May 3, 2019 at 3:02
  • @ben Wilson There’s an answer for that now. Scroll down.
    – Alan H.
    Dec 19, 2020 at 18:14

4 Answers 4

100

If you are using zsh, append these two lines to ~/.zshrc after line source $ZSH/oh-my-zsh.sh

unsetopt inc_append_history
unsetopt share_history

From zshoptions(1) - Linux man page :

INC_APPEND_HISTORY

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.

SHARE_HISTORY

This option both imports new commands from the history file, and also causes your typed commands to be appended to the history file (the latter is like specifying INC_APPEND_HISTORY).

The poster has indicated that this was rather the option he was looking for:

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.

3
  • 4
    someone give this person a raise!
    – jaydel
    Feb 13, 2019 at 14:13
  • working for me. May 19, 2020 at 12:48
  • 3
    Make sure you put this ^ after this line source $ZSH/oh-my-zsh.sh otherwise, these options are overwritten Aug 12, 2020 at 10:16
12

Extra info to the perfect answer of harrymc.

This is not Iterm2 but a feature of zsh which gets activated by default if you use oh-my-zsh

(see: https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/history.zsh)

So put these in your .zshrc file after source $ZSH/oh-my-zsh.sh (see harrymc's answer)

unsetopt inc_append_history
unsetopt share_history
1
  • 1
    after source $ZSH/oh-my-zsh.sh <-- That's the key, thanks!
    – Qback
    Dec 8, 2020 at 10:05
2

You can set your HISTFILE environment variable to something unique, but what I do is simply unset it in my .bashrc file:

export HISTFILE=""

but I don't care to keep my history in a file. By unsetting, history is just kept in memory.

1

For Bash users, just add these two lines into your ~/.bashrc

shopt -s histappend
PROMPT_COMMAND="history -a;history -c;history -r;$PROMPT_COMMAND"

Make sure you open a new tab for this to take effect

1
  • 1
    Can any bash users confirm or correct the answer here as needed? It looks useful but is sitting at a net vote score of zero, which suggests maybe it doesn’t work. Some clarity would be great
    – Alan H.
    Dec 19, 2020 at 18:13

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.