13

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?

michael
  • 5,945

3 Answers3

17

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.
Francisco
  • 2,298
  • 21
  • 21
14

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 in the tab i am searching in.

rrosa
  • 240
  • 2
  • 6
  • This was exactly my problem. The accepted answer wasn't helpful in my case. – Arad Alvand Jul 01 '22 at 13:19
  • This was my problem too. You should try to highlight that you need to press enter in the tab you're searching in. I had first read your answer as "press enter after the command". – owensmartin Aug 29 '22 at 14:41
1

The problem might have been that you shouldn't set INC_APPEND_HISTORY if you're using SHARE_HISTORY - mentioned in the zsh options docs (search for "SHARE_HISTORY").

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

INC_APPEND_HISTORY is used to share history between shells, while allowing you more control about when new history commands get imported. A nice tip from the same docs:

If you find that you want more control over when commands get imported, you may wish to turn SHARE_HISTORY off, INC_APPEND_HISTORY or INC_APPEND_HISTORY_TIME (see above) on, and then manually import commands whenever you need them using ‘fc -RI’.

mrsquee
  • 11
  • 1