Please bear with me, this is not just another question about basic history control options, and I think it has a solid use case.
use case
I have many instances of zsh
running (tmux
with tmux continuum and resurrect). I have these requirements:
requirements
I would like to be able to have isolated histories for each instance of zsh, meaning I only want each zsh to read the history file upon startup, not when I search for past commands (thus, I don't want sharehistory
set). The reason for this is that, while I'm working on work stuff in one zsh (a pane in tmux), I don't want commands from another session (for managing my music, say, or even from another zsh in my work "context") to start appearing there.
However, sometimes I'm in a given zsh, and can't remember which zsh (a pane in tmux
) I ran a desired command in, and I just want to run it again, from the current zsh. Thus, I would like to be able to access the commands from other instances of zsh
right after/as they are run, but only with the history
command, or some suitable alternative, and not with a reverse-incremental history search (Ctrl+R) or in the history stack for that zsh (e.g. Ctrl+P
).
So history | grep "some command from another zsh that I ran after this zsh was started"
should find the desired command from the other zsh, but Ctrl +R
should not, and nor should the history stack in the current zsh.
research and attempts
Based on this, unsetopt sharehistory
is required to satisfy requirement 1., and setopt incappendhistory
is one needed aspect for requirement 2, but this is not all that is needed.
I could simply grep '<regex>' ~/.zsh_history
, and expand the desired line with !
when I found it, but I don't want this.
I could write a custom shell script myspecialhistory.sh
that grepped ~/.zsh_history
, and drop a symlink myspecialhistory
to it in ~/bin
, which is in my path, and use that, but I'd like to know if there's a way to do this natively in zsh.
Note that if I do something like exec $SHELL
(replacing the shell with a new instance), I'll get the entire shared history of all my sessions, assuming inappendhistory
is set in them, so I don't want that: I still want this shell to maintain its own independent history line, with small additions from other sessions' commands, when I use the desired method that is the subject of this question.