73

In Bash I know putting a space before a command prevents it from being kept in the history, what is the equivalent for the zshell?

bneil
  • 1,967
  • 3
  • 16
  • 20
  • cross-linking to (slightly more involved) bash answer: http://unix.stackexchange.com/questions/6094/is-there-any-way-to-keep-a-command-from-being-added-to-your-history – Michael Shigorin Oct 26 '15 at 10:53

2 Answers2

105

Use the HIST_IGNORE_SPACE option.

setopt HIST_IGNORE_SPACE

man zshoptions

HIST_IGNORE_SPACE

Remove command lines from the history list when the first character on the line is a space, or when one of the expanded aliases contains a leading space. Note that the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line. If you want to make it vanish right away without entering another command, type a space and press return.

Darren Hall
  • 7,568
  • 2
  • 28
  • 23
  • I wish I could do this for all-the-commands-typed-in-a-terminal-window-since-open on OSX. For when you realize that you've been barking up the wrong tree. – Dan Rosenstark Nov 01 '11 at 22:57
  • 13
    This wasn't working for me, until I read the whole text and realized it's awesomer than bash!! – 0fnt Aug 19 '14 at 09:51
  • 5
    And then, for the commands I wanted to prevent, I used aliases and prefixed a space: alias jrnl=" jrnl" – Sagar Jauhari Sep 23 '14 at 21:26
  • Is there a way to do this for a block of commands? – CMCDragonkai Apr 26 '15 at 03:54
  • 2
    @Yar kill -9 that shell (if it doesn't do line-per-line history file writes) – Michael Shigorin Oct 26 '15 at 10:47
  • 1
    @MichaelShigorin Thanks! `kill -9 $$` is indeed fantastic to avoid all the commands issued in current terminal session to be stored in the history. – Jeffrey Lebowski Mar 14 '16 at 17:11
  • 2
    @CMCDragonkai You could `unset HISTFILE` prior to your block of commands. You'd need to reset `HISTFILE` afterward or open a new shell to keep commands in your history that you want. – Chauncey Garrett Jun 29 '16 at 18:36
  • Small "warning" if you test this locally. If you do a command like `_blah` (`_` as space) to "test" then press the up key or do `Ctrl` + `R` right away or before entering a different command (besides like just pressing enter) then you'll still see the `_blah` there -- but once you run a "real" command it will not be. Just a heads up because sometimes I do this to make sure things don't end up in history and forget this behavior then I get worried. (It's possible this is a bug?) – Captain Man Oct 15 '20 at 18:13
  • @CaptainMan -- as it says right in the answer - "the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line." Not a bug. – Zac Thompson Feb 09 '21 at 09:07
  • `unsetopt HIST_IGNORE_SPACE` – cmcginty Apr 13 '21 at 20:52
  • Why this config is not set by default? – Felipe Mar 07 '22 at 23:16
13

If you desire more granular control over what's added to ZSH history, you can define the zshaddhistory function in .zshrc. The following definition uses a regex to define a pattern to ignore:

function zshaddhistory() {
  emulate -L zsh
  if ! [[ "$1" =~ "(^ |^ykchalresp|--password)" ]] ; then
      print -sr -- "${1%%$'\n'}"
      fc -p
  else
      return 1
  fi
}

Note that the behavior from man zshopts under HIST_IGNORE_SPACE is still present:

Note that the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line.

So to test it, you would have to hit an extra [Enter]. This removes the command both from the output of history, and also the ↑ arrow history.

See this plugin for keeping log entries, but mask sensitive data based on a regex: https://github.com/jgogstad/passwordless-history

gogstad
  • 231
  • 2
  • 5