28

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).

35

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.

Related for bash:

  • 4
    "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. – artfulrobot 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. – Linus Arver 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. – Pablo Bianchi Oct 5 '17 at 21:16
4

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. – Cees Timmerman Oct 13 '16 at 12:36
  • @CeesTimmerman fixed, thank you. It's important for communication in general I believe. What did you mean by "Important for shell work"? – james_womack Oct 13 '16 at 19:01
  • 1
    A typo in the shell can corrupt your entire system. – Cees Timmerman Oct 13 '16 at 21:52

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.