1

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:

  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 global 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 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 that zsh (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.

Life5ign
  • 113
  • A user on a former post suggested "I wanted to do this myself and found the command 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:12
  • 1
    You do need to build that wheel (if someone hasn't done it already) but you get more building blocks. In particular fc -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
  • 1
    https://github.com/jimhester/per-directory-history/blob/master/per-directory-history.zsh is not quite the same, but you can probably reuse parts of that code, or at least use it as a source of inspiration. – Gilles 'SO- stop being evil' Jan 11 '24 at 21:30
  • @Gilles'SO-stopbeingevil' thanks, that's an excellent reference. wow zsh is vast, and based on that, I'm sure it's possible. The per-directory is an interesting implementation, I almost wonder if it would work for my needs. If I had multiple zsh open in the same directory ("project context" if you will), I'd want those to share the "local" history. And if a new zsh was opened, it would be either for a) random, system wide commands, in which case I'd set global history, or b) for "contextual" commands, in which case I'd set per-directory history, which is the default in that setup. – Life5ign Jan 12 '24 at 14:56
  • useful related post (per directory history, oh-my-zsh plugin, and another plugin): https://unix.stackexchange.com/questions/71189/per-directory-history-in-zsh – Life5ign Jan 12 '24 at 15:02

0 Answers0