Score:1

zsh: access history from other zsh sessions, but only when I want to, not by default; e.g. different behavior for "history" command and Ctrl+R

us flag

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

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

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

terdon avatar
cn flag
This question is 100% on topic and welcome here, but if you don't get an answer after a while, you might want to ask on [linux.se] instead because that site has a higher concentration of zsh experts. If you do decide to ask there, just remember to delete this first. And do wait a while since there are zsh people here too!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.