zsh - 如何让 zsh 从历史记录中排除某些命令?

标签 zsh zshrc

例如,那些包含“生产”一词的内容?

最佳答案

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

将上述内容放入交互式 shell 启动时将获取的文件中(放入 .zshrc 或像我一样从 .zshrc 获取的文件)。

替代形式(隐式添加到历史记录中):

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

。注:

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

显式地将项目添加到历史记录中。但是,如果 zshaddhistory 以零退出代码返回,那么 zsh 也会隐式执行相同的操作,因此无需使用 fc -p 并使用 setopt nohistignoredups nohistignorealldups (这是默认状态)您将在历史记录中看到不需要的重复项。

emulate -L zsh 用于确保模拟设置不会介入并更改函数体的解释。我将此行放在我在 zsh 配置中定义的每个函数的开头。

关于zsh - 如何让 zsh 从历史记录中排除某些命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6934565/

相关文章:

bash - ZSH 脚本和提示分析?

zsh完成差异

zsh - 由于 zsh 中的日期命令,多行提示格式不正确

bash - shell 更新后在 .bashrc 中找不到 shopt 命令

zsh - RVM 在 ZSH 中显示 Ruby 版本

ZSh 中的 Mercurial 分支/文件名补全

shell - Makefile 命令与文件夹名称冲突

Zsh:智能自动完成功能?

swift - 在 swift 脚本中运行 shell 命令

android - "andr"选项卡完成为 "_xrandr"而不是 android 或什么都没有