marlonrichert / zsh-hist

📝 Edit your Zsh history from the command line.
MIT License
148 stars 13 forks source link

Make plugin work with per-directory-history too, not just global history #24

Open cohml opened 9 months ago

cohml commented 9 months ago

What do you want?

per-directory-history is a very simple yet extremely nice zsh plugin which allows users to track not only "global" history, but also history on a per-directory basis. In other words, if I run cd ~ from ~/foo, cd ~ would be recorded in my global history, but also in a dedicated file containing only those commands execute from ~/foo. This is useful because often commands run from a given location are related, so being able to search only among them helps filter out noise and find the desired commands quickly.

Anyway, zsh-hist currently seems to only work with global history, which makes it only a partial solution for the users of per-directory-history.

Therefore modifying zsh-hist to be sensitive to history mode, such that commands are removed only from the currently-active history file (or perhaps even better, from both global and per-directory histories?) would be a major usability upgrade.

Why do you want this?

Because from what I can tell, zsh-hist doesn't do anything with per-directory histories, rendering it only half useful for per-directory-history users.

Who else would benefit from this?

Anyone who uses per-directory-history or equivalent plugins.

For the record, per-directory-history is one of the plugins that ships natively with Oh-My-Zsh, so the user base is potentially large.

How should it work?

Persumably zsh-hist would first identify the current history mode, and accordingly the associated history file. Any deletions/updates/etc. would then be applied to that file, at a minimum.

Ideally they would also be applied to the global file, so that everything in (or not in) a local file is also in (or not in) the global file, but this is just "nice to have" since I realize it may be a heavy lift.

cohml commented 9 months ago

The simplest implementation may be to implement an option whereby one can specify the path to an arbitrary history file (provided of course that it is a zsh-formatted/compatible history file), with HISTFILE being the default value.

cohml commented 9 months ago

Following up: I managed to get it working with both global history ($HISTFILE) and also per-directory history by manually overriding HISTFILE when executing hist, e.g.

# delete from global history
HISTFILE="${HISTFILE}" hist delete 123  # default behavior, equivalent to just `hist delete 123`

# delete from local history
HISTFILE="${_per_directory_history_directory}" hist delete 123

_per_directory_history_directory points to my PWD-specific history file, and gets set by the per-directory-history plugin. So the code above obviously won't work in all cases.

But it illustrates a general (if hacky) approach that will work for others wanting this feature without requiring any modifications to the source code.

[!NOTE]
There's one drawback though: If I want to delete a command from all histories (i.e., global and local), I still need to use hist delete twice, one for each history mode/file. But fixing that, such that a single call to hist delete removes a command from everywhere, would require modifying the source code.

Ideally this feature will be implemented directly into zsh-hist, so I'll leave this issue open. But pending that, this works for me as a temporary stopgap.