6

Like, say, those that include the word "production"?

John Watts
  • 8,717
  • 1
  • 31
  • 35
John Bachir
  • 22,495
  • 29
  • 154
  • 227
  • In case you were like me looking for a way to mark a command/alias so that it is filtered out of history, `zsh` can optionally exclude some commands/aliases if they start with a space : https://superuser.com/q/352788/1067306 – cassepipe Mar 23 '23 at 18:30

1 Answers1

8
function zshaddhistory() {
    emulate -L zsh
    if [[ $1 != *"production"* ]] ; then
        print -sr -- "${1%%$'\n'}"
        fc -p
    else
        return 1
    fi
}

Put the above to a file that will be sourced when interactive shell starts (to .zshrc or to a file that is sourced from .zshrc like I do).

Alternate form (implicit addition to history):

function zshaddhistory() {
    emulate -L zsh
    if [[ $1 = *"production"* ]] ; then
        return 1
    fi
}

. Note:

print -sr -- "${1%%$'\n'}"

explicitly adds item to history. But the same thing implicitly does zsh if zshaddhistory returns with zero exit code, so without fc -p and with setopt nohistignoredups nohistignorealldups (which is the default state) you will see unneeded duplicates in history.

emulate -L zsh is here to make sure that emulation settings does not step in and change the interpretation of the function body. I put this line at the start of the every function I define in zsh configuration.

ZyX
  • 52,536
  • 7
  • 114
  • 135
  • 5
    this isn't working for me... i've tried many permutations. i've also tested that the branches are getting entered correctly (the `if` condition is correct). but the command still gets put into history. But this works: `zshaddhistory() { [[ $1 != *production* ]] }` – John Bachir Aug 12 '11 at 23:51
  • 1
    @John Bachir I made a mistake: I thought that `fc -p` is related to adding command to a history (and now unsure what is it doing here at all). Your variant works because according to the help `If any of the hook functions return a non-zero value the history line will not be saved, although it lingers in the history until the next line is executed allow you to reuse or edit it immediately.`. – ZyX Aug 21 '11 at 00:28
  • Could you explain why you need the `print` and `fc` in the first place? I just tested it myself and it looks like the line (which triggered `zshaddhistory`s call) will be added to history automatically once this function returns 0. – Julian Sep 20 '15 at 12:18
  • 1
    @Julian It appears that you either need to remove those lines *or* use *both* `print -sr` and `fc -p`. `fc -p` *is* needed because otherwise `print` will produce a duplicate entry (which will then be removed by HIST_IGNORE_DUPS option if it is set, so you will not see it most of time). I.e. my code explicitly adds entry to the history, removing it or replacing with `return 0` will do just the same thing implicitly. – ZyX Sep 20 '15 at 16:25
  • 1
    @Julian I updated the answer with more explanations. – ZyX Sep 20 '15 at 16:32
  • I'm trying to remove the 'r' command (as it doesn't do anything but repeats the last cmd), but this is not working for me. I replaced the str with 'r' and removed the stars.. – Rainy Jul 23 '21 at 15:30
  • 1
    @Rainy You are probably missing LF, there is a reason my first function contains `${1%%$'\n'}`. – ZyX Jul 24 '21 at 20:09
  • @ZyX, no - I pasted it exactly like you have it: # exclude 'r' cmd from history function zshaddhistory() { emulate -L zsh if [[ $1 != "r" ]] ; then print -sr -- "${1%%$'\n'}" fc -p else return 1 fi } (SO doesn't format it properly in comment but it's pasted directly from your answer) – Rainy Jul 25 '21 at 12:36
  • 1
    @Rainy You have missed the point. `$1` *ends with* `\n`, so matching `$1` against `r` will fail because it is never equal to *just* `r`. Asterisks covered that, your code does not. – ZyX Jul 31 '21 at 20:55