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:
I would like to be able to have isolated histories for each instance of
zsh
, meaning I only want eachzsh
to read the global history file upon startup, not when I search for past commands (thus, I don't wantsharehistory
set). The reason for this is that, while I'm working on work stuff in onezsh
(a pane intmux
), I don't want commands from another session (for managing my music, say, or even from anotherzsh
in my work "context") to start appearing there.However, sometimes I'm in a given
zsh
, and can't remember whichzsh
(a pane intmux
) I ran a desired command in, and I just want to run it again, from the currentzsh
. Thus, I would like to be able to access the commands from other instances ofzsh
right after they're run (from the global history), but only by some alternative means, and not with a reverse-incremental history search (Ctrl+R) or in the history stack for thatzsh
(Ctrl+P). Because those should be reserved for the "local" history only.
So the solution should find the desired history from other zsh
instances, 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 write a shell function history_get_global
that grepped ~/.zsh_history
(since I have incremental history append set, all new entries will be there) and expanded them somehow, to let me choose, but I'd like to know if there's a way to do this natively in zsh
, without reinventing the wheel.
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. Perhaps, even the usage of the global history wouldn't get pushed to the local history stack, so the local zsh history stack would remain isolated.
fc -R
(this is a ZSH built-in command) to do exactly that. It re-reads the history from.zsh_history
and loads it in the current shell." which is close, but it clobbers my "isolated history" in the current instance of zsh, which I don't want – Life5ign Jan 10 '24 at 20:12fc -p -a
to temporarily use a different history list so you can use zsh's history loading and parsing code. – Gilles 'SO- stop being evil' Jan 11 '24 at 21:27