-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hstr shouldn't use .bash_history as data source and not advertise to use "history -n" in PROMPT_COMMAND #400
Comments
Oh and in addition: I cannot think of a counter example right now but maybe when the alias is then used in combination with other shell syntax like compound commands. I forgot the So perhaps it would be better to use constructs like: |
Thank you for the comprehnesive analysis Christoph! You are right, sending I will redesign I really appreciate your interest in |
Thanks for your kind words :-) The problem with using the history file is generally that the shell probably doesn't expect anyone to do this. So there might be all kinds of weird bugs, e.g. when the shell truncates the file and so on. Before you invest a lot of time in redesigning, perhaps it makes sense to reach out for bash/zsh upstream, whether they'd be interested in you directly integrating hstr into the respective shell. Oh and btw: It wasn't me who packaged hstr for Debian, the maintainer is Daniel Echeverri. |
Great to know this is on the roadmap. Before finding this issue, I was thinking of a similar idea, sending just the in-memory history in through a pipe and appending that to the data from the history file. Not using the file directly at all seems cleaner though. I want this because the idea of writing to my SSD every time I hit enter makes me twitchy. A silly concern, most likely, but hey. |
just wondered, whether this is still considered? :-) @dvorka |
Just to add to the conversation, there are more edge cases to keep in mind if you're going to implement this. The output of I suggest taking a look at what For example, they use the |
Isn't that also already the case when the history is read from the file? |
Temporary workaround: |
But then one effectively looses persistent history?! |
I have a somewhat elaborate setup where history is being read from somewhere else in bashrc and processed. I assumed that was the point of the thread, doing advanced things with the history and still having hstr work. |
@calestyo @Gooberpatrol66 Hey hello! Yes, I think that this feature makes sense (it might be either configurable option OR replace existing implementation), however, I'm unfortunately busy and don't have enough time to implement it. Apologies! |
We received a related report at ohmybash/oh-my-bash#422. The strange behavior that we observed there turned out to be caused by the settings instructed by I'm not sure what is the original intent of Possible solutions are:
The harm of
After three years, I still find
As described above, it is not robust and causes a problem, unfortunately. |
Hey @akinomyoga. Every time I've "met" you so far, you proved have extremely deep knowledge of bash and the stuff around it ... so most likely what you say is what should be done, but I don't quite understand it ^^
This is the "partial solution" I've looked at in the original report.
I still think the main problem here is that |
Also, I don't think anything has been done in the meantime to get that fixed.
What do you think would be the problem if using the output from |
Ah, OK. So "the |
This seems like a more appropriate issue to point out a really nice feature that I have heard many would find very useful - timestamps next to the command history indicating the most recent time the command was executed. This was also mentioned in #128, at least I think that's what they are asking for. Reading this issue makes you realize the built in bash history is pretty limited considering the features that could be added to hstr if they were supported by it, such as:
|
This doesn't seem to be related to the issue originally discussed here, or is it? I guess it might make more sense to have this in a separate issue. |
Because timestamps are not automatically stored in the bash history if the method of accessing and updating the history is re-tooled to in-memory methods then support for timestamps would likely need to be factored into that. I have not fully researched the functionality of bash history timestamps, so you could be correct @calestyo that this belongs in a separate issue. |
btw: Until this is fixed in |
To not have hstr append history items from each terminal immediately to disk (PROMPT_COMMAND="history -a; history -n), but read the history from the in-memory history of terminal it's running in, you can use something like:
Notice the usage of _hstr in the bind command and the commenting of the PROMPT_COMAND. fzf is good, but inferior to hstr for searching history conveniently. |
Hey.
Not sure whether the following would really work in the end, but perhaps it's a better way of handling things.
AFAIU there are actually two histories...
history
, bash's Ctrl-r or UP-key..bash_history
), more or less "shared" by all open bash shell processes and either overwritten or appended to when a shell process quits (or when callinghistory
with some parameters).Also, AFAIU, there are basically two ways to use
hstr
:PROMPT_COMMAND="history -a; history -n"
Each of them has problems associated:
(1):
When not using
PROMPT_COMMAND="history -a; history -n"
, then newly entered commands in this shell won't get appended to the history file until the shell quits.This also means, that any invocation of
hstr
won't see those commands, which is obviously bad.(2):
When using
PROMPT_COMMAND="history -a; history -n"
, then newly entered commands will get appended to the history file immediately.While this solves the problem of not seeing newly entered commands of this shell it makes things pretty messy when using multiple shells:
Example, with two shells (the comment after each command is the order in which commands were entered in the two shells):
Shell I:
Shell II:
Looking at the actual
.bash_history
things look as expected:But looking at the in-memory-histories of the two shells (which is what will also be used for e.g. the UP-key) things are pretty messed up:
Shell I:
Shell II:
Some entries are doubled, some are even missing. Strangely they're still missing when entering further commands (so it's not just one further missing reading of the history file), guess that's because of how bash appends/merges the history to/from the history file.
The whole thing can get even quite disastrous when people don't expect this behaviour, especially when e.g. multiple people use the same user account (role accounts like root), consider in chronological order e.g.:
User A:
$ date
User A:
# presses UP key, gets date again, presses ENTER
User B:
$ cd tmp
User B:
$ rm -rf *
User A:
# presses UP key, gets date again, presses ENTER
It's not so unlikely, that A does this so fast (not expecting rm -rf might show up, that he actually executes
rm -rf *
wherever he is.A partial solution for this might be to not advertise people to use:
PROMPT_COMMAND="history -a; history -n"
but rather just
PROMPT_COMMAND="history -a"
that way changes from other shells wouldn't be read in again (only for newly started shells).
This has however still the behaviour that commands are immediately appended to the history file, which has the following properties:
Since hstr uses the history file as data source, commands from other shells will show up immediately in this shell
This can be both, an advantage:
and disadvantage:
User A: might expect "Ctrl-r f ENTER" to execute his most recently entered "
firefox --profile foo & exit
", while he'll actually get User B's "rm -rf" which was more recent in the history file.I think the disadvantage overweights by far,... it's effectively the same problem as above just with hstr and not the UP key or Bash's Ctrl-r (after all, a user might have kept Bash's Ctrl-r for use and bound hstr to another key).
I think the following could possibly solve this:
Don't use the history file at all as a data source, except perhaps when deleting commands from it.
Instead have someting like like "history | hstr --" bound to the key.
hsrt would then need to read the history from stdin (removing a portion of something like "
^\(\*\|\)[[:digit:]]* *
" from it's beginning - the history entry numbers might be prefixed with*
).The same would need to be done for directly invoking hstr, e.g. something like
alias hstr='history | hstr'
Has the advantage of not getting messed up with history file manipulations of other shells, while still seeing new commands in this shell.
No need to set any
PROMPT_COMMAND
at all, no need to sync the file after each command (which can easily cause defragmentation especially on CoW filesystems).Deleting an entry would also need to be done via the buil-tin
history -d offset
and no longer affect the histories of other shells unless these are re-loaded.But I think it would have the advantage of not having to write to the history file, which bash probably doesn't expect anyone elese to ever do.
Cheers,
Chris.
The text was updated successfully, but these errors were encountered: