How can it be achieved so that every command that is entered, is visible in every open terminal's history?

When having multiple terminals open, the history isn't shared, i.e. what you entered in one terminals history, doesn't show up in another one. Think of an alternative to Bash's PROMPT_COMMAND="history -a" (which saves the history before the prompt is beeing displayed).


The following options would be applicable:

  • To save every command before it is executed (this is different from bash's history -a solution):

     setopt inc_append_history
  • To read the history file everytime history is called upon as well as the functionality from inc_append_history:

     setopt share_history

These can be set in your .zshrc file.

⚠️ Either set inc_append_history or share_history but not both. (see comments bellow)

  • When share_history is enabled, it reads and writes to the history file.
  • When inc_append_history is enabled, it only writes to the history file.

Related for bash:

  • 7
    "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, which should be turned off if this option is in effect)." (From manual emphasis added) i.e. just the INC_APPEND_HISTORY option will do what you want. Oct 20 '14 at 10:30
  • 3
    Actually, just the SHARE_HISTORY option alone will do what you want. The phrase "which should be turned off if this option is in effect" translates to "[INC_APPEND_HISTORY] should be turned off if [SHARE_HISTORY] option is in effect". I've tested this on zsh 5.1.1 on Ubuntu Xenial. Feb 2 '17 at 0:33
  • 1
    @opert is correct: setopt share_history is what will accomplish the desired result. @MJB would you be willing to edit your answer to remove the mention of setopt inc_append_history? It obscures the actual answer.
    – bonh
    Feb 22 '17 at 16:40
  • Names are case insensitive and underscores are ignored so if you setopt | grep hist will get incappendhistory and sharehistory. Oct 5 '17 at 21:16
  • Just to clarify. You write command in shell 1, then switch to shell 2 and press enter. You will see that command as last only in case of share_history, but not inc_append_history. Jan 3 '20 at 17:52

If you use Robby Russell’s awesome OhMyZSH, it will take care of this and more.

See https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/history.zsh

That includes setopt inc_append_history.

  • It's his/hers/its. Important for shell work. Oct 13 '16 at 12:36
  • 1
    @CeesTimmerman fixed, thank you. It's important for communication in general I believe. What did you mean by "Important for shell work"? Oct 13 '16 at 19:01
  • 2
    A typo in the shell can corrupt your entire system. Oct 13 '16 at 21:52
  • should I paste it into .zshrc?
    – alper
    Jul 20 '20 at 15:58

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.