如何从 shell 终端历史记录中删除未知命令 (zsh)
How to remove unknown commands from shell terminal history (zsh)
我正在尝试在 zsh 中设置我的历史记录。我已经激活了像 HIST_IGNORE_ALL_DUPS 这样的选项,它删除了历史记录中的重复命令。
但我也在寻找一些选项,可以删除不存在的命令 return 127“找不到命令”。
Zsh 中没有这样的选项,但是可以通过 zsh-hist
plugin:
轻松实现
autoload -Uz add-zsh-hook
command-not-found () {
# -f: force
# -s: silent
# d: delete
# -1: most recent history item
(( ? == 127 )) && hist -fs d -1
}
add-zsh-hook precmd command-not-found
这将自动从历史记录中删除最后一个项目,如果它返回 127
。
或者,除了删除它之外,您还可以将删除的命令加载到编辑缓冲区中,这样您就可以立即更正您输入的任何拼写错误,方法是使用 hist f
而不是 hist d
:
autoload -Uz add-zsh-hook
command-not-found () {
# f: fix
(( ? == 127 )) && hist -fs f -1
}
add-zsh-hook precmd command-not-found
我正在尝试在 zsh 中设置我的历史记录。我已经激活了像 HIST_IGNORE_ALL_DUPS 这样的选项,它删除了历史记录中的重复命令。
但我也在寻找一些选项,可以删除不存在的命令 return 127“找不到命令”。
Zsh 中没有这样的选项,但是可以通过 zsh-hist
plugin:
autoload -Uz add-zsh-hook
command-not-found () {
# -f: force
# -s: silent
# d: delete
# -1: most recent history item
(( ? == 127 )) && hist -fs d -1
}
add-zsh-hook precmd command-not-found
这将自动从历史记录中删除最后一个项目,如果它返回 127
。
或者,除了删除它之外,您还可以将删除的命令加载到编辑缓冲区中,这样您就可以立即更正您输入的任何拼写错误,方法是使用 hist f
而不是 hist d
:
autoload -Uz add-zsh-hook
command-not-found () {
# f: fix
(( ? == 127 )) && hist -fs f -1
}
add-zsh-hook precmd command-not-found