2

I am trying to convert some bash dotfiles to their fish equivalents.

I have a ~/.bash_logout that looks like this:

# Flush the in-memory history, then persist the emptiness to disk.
history -c && history -w;

While fish does have a history command, it does not have the same options, so the above will not work as-is in fish.

The closest I have gotten so far is history clear, though this only seems to apply to the current session and is not persisted, as the history comes back in any new tabs I create. Further, it prompts for confirmation and I would prefer not to have to do that every time I log out.

Nonetheless, it is progress, so based on another answer, I put this in ~/.config/fish/config.fish:

function on_exit --on-process %self
    history clear
end

This doesn't seem to even prompt me, let alone do anything useful. Or if it does, it's non-blocking and happens so fast that I can't see it.

How can I permanently erase the fish shell history and persist that state for future sessions, within an exit handler? Existing sessions would be nice too, but are less important.

Lastly, the history manpages notes that the "builtin history" does not prompt for confirmation. Does it make sense to use that instead and if so, how?

· clear clears the history file. A prompt is displayed before the history is erased asking you to confirm you really want to clear all history unless builtin history is used.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Seth Holladay
  • 8,951
  • 3
  • 34
  • 43

1 Answers1

8

For me, history clear seems to apply across sessions and persists through startup. Have you updated to the latest version or FiSh ? I’m using version 2.6.0.

    echo $version

To remove the confirmation prompt, you can edit the function directly (funced history), removing lines 188-195 sparing 191-192. Although, all this actually does is ultimately run the command builtin history clear, so this will achieve exactly the same thing minus the confirmation. Simply type:

    builtin history clear

(which, again, for me, does seem to go across sessions and is persistent.)

Regarding the event handler, the reason the exit handler doesn’t fire is because %self gets expanded into the PID of the currently running shell process. However, once that exits and a new one starts, the event handler won’t fire as the PID will be different.

So this function will fire before on exiting the currently running shell process:

    function bye --on-process-exit %self
        echo byeeeeeeee
    end

but only that process and not any subsequently created process. Instead, add the function to your config.fish file, which will ensure it initialises the event handler each startup using the correct and current PID. The config file, if it exists, is located at ~/.config/fish/config.fish If it doesn’t exist, ensure the environment variable XDG_CONFIG_HOME is set, which tells fish where to find the config files. You can set it yourself and export it globally with:

    set -gx XDG_CONFIG_HOME ~/.config/

Then create the config file with the event handler:

    function bye --on-process-exit %self
        builtin history clear
        echo Session history scrubbed.  Goodbye
    end

Done. The only history item remaining will be the command exit used to end the current shell session.

Hope this helps. Seems to work for me but if you run into problems, I might be able to point you to clues for some answers.

CJK
  • 5,732
  • 1
  • 8
  • 26
  • 3
    Nice, `builtin history clear` works how I want, in that it doesn't prompt me for confirmation. I also tested the new session case again and you are right. Yet, for some reason, there is a set of commands that won't go away; things that I ran soon after setting up Fish (probably in my first session). Everything since then gets cleared across sessions. But when I make a new tab and hit the up arrow, these dozen or so commands are there (only in the new tab, not the current one). That led to my confusion earlier. Any idea what's going on there? Seems like a bug. I'm on fish 2.7.0. – Seth Holladay Dec 23 '17 at 18:08
  • Thanks for your help, I accepted the answer as it was very useful. If you do find out anything about the history that stubbornly refuses to go away, please do let me know. I haven't been able to fix that. It also annoys my OCD that `exit` remains in the history, especially because that doesn't happen with the bash equivalent. – Seth Holladay Dec 29 '17 at 10:37
  • There also is a specific `fish_exit` event: `function on_exit --on-event fish_exit` ([source](https://fishshell.com/docs/current/index.html#initialization-files)) – Harm Sep 22 '20 at 21:56
  • @HarmteMolder There wasn't in 2017, which is when this answer was written. – CJK Sep 23 '20 at 03:44
  • @CJK No worries, I just thought I'd leave that there for future readers – Harm Sep 23 '20 at 17:51