2

These two questions address how to keep a given command from being added to the shell history:

The answers focus on solutions where the user prefixes each command (e.g. with a space). What I am looking for is a single command that I can run once, and that would effectively "freeze" my history file in the shell until I unfreeze it later with another command.

While the history is "frozen":

  1. I want my commands to be able to be able to read from the existing history file (e.g. so that I can do command searches e.g. C-r, and standard autocompletion.

  2. I want to prevent any commands I run from being added to the history

How can I accomplish this with zsh?

1
  • 1
    would setting HISTORY_IGNORE to something like * be in the right direction? – Jeff Schaller Sep 8 '17 at 16:17
1

You can use fc -p to start a new history list, and fc -P to recall the previous history list. After a plain fc -p, you get a blank history. You can pass a file name to fc -p to read the history from that file, so save the history and immediately read it back with fc -p. When you call fc -P, it restores the value of HISTFILE from before the call to fc -p.

freeze_history () {
    fc -A
    fc -p $HISTFILE
    unset HISTFILE
}
unfreeze_history () {
    fc -P
}

Unfortunately the history can't be edited directly in memory, you can only populate it artificially by reading from a file.

0

These might help: You can type unset HISTFILE to turn off history for the session. Logging out and back in will start it up again.

Also, you can return to a command in history and erase it by hitting CTRL-u.

Good luck!

2
  • Does unsetting HISTFILE prevent reading previous commands from history? – Jeff Schaller Sep 8 '17 at 17:06
  • @JeffSchaller No, it doesn't. However, HISTFILE only matters at the time you exit the shell or save the history explicitly. If you unset it then set it again, the history from the time it was unset is kept. – Gilles 'SO- stop being evil' Sep 8 '17 at 22:10
0

I use following aliases in my .zshrc:

alias disablehistory="function zshaddhistory() {  return 1 }"
alias enablehistory="unset -f zshaddhistory"

So I can run disablehistory and no new command will enter history from my current session. Then, I can run enablehistory.

How it works? disablehistory defines history hook function.

If any of the hook functions returns status 1 (or any non-zero value other than 2, though this is not guaranteed for future versions of the shell) the history line will not be saved, although it lingers in the history until the next line is executed, allowing you to reuse or edit it immediately.

So it instructs zsh to always reject history entry.

enablehistory unsets (-f stands for function) zshaddhistory function, so default behavior returns.

New contributor
Ginden is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.