Clear fish shell history permanently across sessions

9.1k views Asked by At

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.

1

There are 1 answers

5
CJK On BEST ANSWER

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.