Share history in multiple zsh shell

I am trying to setup zsh so that it shares command history between different zsh sessions:

  • in multiple tabs
  • in multiple gnome-terminals
  • in different screen sessions

I have put this in .zshrc

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

#To retrieve the history file everytime history is called upon.
setopt share_history

but that does not work.

e.g. I type 1 command: gedit afile and then I go to and zsh and type history. I don't see gedit afile.

output of 'setopt' is

 % setopt
nohistbeep
histexpiredupsfirst
histfindnodups
histignorealldups
histignoredups
histignorespace
histnostore
histreduceblanks
histsavenodups
histverify
incappendhistory
interactive
monitor
promptsubst
sharehistory
shinstdin
zle

How can I achieve this?


The simple answer to your question is you need to set share_history, you do that with:

setopt share_history

Since you obviously did that already (and that option actually works). I suggest you to check:

  • whether both shells have the option set;
  • whether you are not typing commands with a leading space (since histignorespace makes those be ignored)
  • Do you have $HISTFILE set to the same value in all shells?
  • whether you are actually saving any history? Say, if you issue echo 123 in tab-1. Go to tab-2, call history. Is it there? (as per your problem, not). Now issue, fc -R (means re-read the history file), and then history is it there now? If not, you may also want to call fc -A (-A will forcefully append your history the file) at tab-1 to make sure history is written to the file.

try searching after pressing enter.


what i tested was:

  • open 2 zsh shells
  • go to zsh shell 1 and execute echo "something"
  • go to zsh shell 2, press enter, then check if you see the command from shell 1.

in my case i will not see the command until i press enter.