开发者

How can I have zsh exclude certain commands from history?

开发者 https://www.devze.com 2023-03-26 05:40 出处:网络
Like, say, those that include the w开发者_C百科ord \"production\"?function zshaddhistory() { emulate -L zsh

Like, say, those that include the w开发者_C百科ord "production"?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消