1

I have noticed that default behaviour for storing command history is only storing the latest entry. Typing ls and then ls -al and then entering history would show:

ls -al
ls

Typing ls once more would then show:

ls
ls -al

All other occurances of these commands in the command history are now gone. This makes it hard to relearn a process I may have done a few days ago.
Is there a way to make fish preserve every command entry in the same order as they are entered?

0

1 Answer 1

1

There's no configuration to do this automatically with the history function itself, no. From the Fish docs:

Any duplicate history items are automatically removed.

However, if the goal is simply to capture the what you entered so that you can review the commands later (in the proper context), that's it's entirely possible via scripting. At a simple level, what you want to do is simply save every command entered at the prompt into a file that doesn't remove duplicates.

Create the following function in ~/.config/fish/conf.d/save_full_history.fish:

function log_command --on-event fish_preexec
    echo $argv[1] >>  ~/.local/share/fish/fish_full_history
end

That does a few things:

  • By having it in ~/.config/fish/conf.d, it will automatically load the function every time Fish starts. This is done through conf.d since --on-event (and other) hooks aren't activated in lazy-loaded Fish functions.
  • Using the --on-event fish_preexec, the function runs just after Enter is pressed for each command.
  • The function receives the full commandline that was entered as $argv[1], saving it to the ~/.local/share/fish/fish_full_history file for future review.

You could, of course, add something like a timestamp to this. E.g.:

echo $(date --iso-8601=ns) --- $argv[1] >>  ~/.local/share/fish/fish_full_history

You could also create a separate function (it can be in the same file) that executed on fish_posterror to make a note in the file when the command resulted in an error.

6
  • That's a pretty cool solution. How would I go about implementing the time stamp? And what editor does Fish use when it shows my history?
    – s_dav
    Commented Oct 13, 2022 at 14:35
  • @attendeapril Added a (simple) sample timestamp line to the answer, but you'll probably want to tweak it. As for the "editor", if you mean what program is used when you just type history in Fish, that's going to be whatever pager is default on your system (typically less). You can override that with the PAGER variable. For instance, if you wanted to use bat instead, you could set -Ux PAGER /usr/local/bin/bat (or whatever pager you want to use). Commented Oct 13, 2022 at 16:16
  • Thank you for your very helpful answer! I have been working on this here and there for a couple of days with somewhat success. This function seems to work: function log_command --on-event fish_preexec echo (date --iso-8601=minutes) $argv[1] >> ~/.local/share/fish/fish_full_history end - But tweaking the date to a cleaner format does not seem to work: function log_command --on-event fish_preexec echo (date +'%Y-%m-%d %H:%M:%S ') $argv[1] >> ~/.local/share/fish/fish_full_history end
    – s_dav
    Commented Oct 18, 2022 at 18:22
  • @attendeapril That's odd. I copied your updated (and yes, "cleaner", I agree) echo statement over to my copy, and it seems to be working just fine. When you say "does not seem to work", can you elaborate? Thanks! Commented Oct 19, 2022 at 1:30
  • 1
    I put it down for a few days and now it works. I suspect it may have been because I didn't restart my terminal application in my testing. Thanky you so much for your great help!
    – s_dav
    Commented Oct 24, 2022 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .