假设我使用zsh运行命令
echo "mysecret" > file
我可以使用以下命令轻松打印包含条目号的历史记录fc -l:
Run Code Online (Sandbox Code Playgroud)1 echo "mysecret" >| file
但是,如何从历史记录中轻松删除条目?
我在man zshbuiltins中找不到相应的段落.
rxw*_*rxw 90
LC_ALL=C sed -i '' '/porn/d' $HISTFILE
Run Code Online (Sandbox Code Playgroud)
LC_ALL=C sed -i '/porn/d' $HISTFILE
Run Code Online (Sandbox Code Playgroud)
这将从$ HISTFILE中删除所有与"porn"匹配的行.
使用setopt HIST_IGNORE_SPACE,您可以在上面的命令前加一个空格字符,以防止它被写入$ HISTFILE.
正如Tim在下面的评论中指出的那样,前缀LC_ALL = C可以防止"非法字节序列"失败.
pop*_*tea 50
我不知道是否有一些优雅的方法可以做到这一点,但在类似的情况下我已经注销(允许zsh清空其缓冲区并将我的历史记录写入文件),然后登录,最后手动编辑 ~/.zsh_history,删除"危险的"线.
Mic*_*ton 24
如果在zsh中使用HIST_IGNORE_SPACE选项,则可以在空格""前面添加命令,并且不会在历史记录文件中记住这些命令.如果你有秘密命令,你通常使用你可以做这些事情:
http://unsyncopated.com/blog/index.php/2007/08/18/omitting-certain-commands-from-zshs-history/
pil*_*gix 13
如果您只想偶尔删除,我认为手动编辑您的.zsh_history.
在 zsh 终端中:
~/.zsh_history使用文本编辑器(pico、Emacs、vim...)打开,history,不需要的历史记录项目就会消失。(确保编辑器没有备份之前的 .zsh_history 实例。)
(基于https://til.hashrocket.com/posts/zn87awopb4-delete-a-command-from-zsh-history-的解决方案)
此功能将从 Zsh 历史记录中删除您想要的任何一行,不问任何问题:
# Accepts one history line number as argument.
# Use `dc -1` to remove the last line.
dc () {
# Prevent the specified history line from being
# saved.
local HISTORY_IGNORE="${(b)$(fc -ln $1 $1)}"
# Write out the history to file, excluding lines that
# match `$HISTORY_IGNORE`.
fc -W
# Dispose of the current history and read the new
# history from file.
fc -p $HISTFILE $HISTSIZE $SAVEHIST
# TA-DA!
print "Deleted '$HISTORY_IGNORE' from history."
}
Run Code Online (Sandbox Code Playgroud)
如果您还想防止所有dc命令写入历史记录,请在~/.zshrc文件中添加以下内容:
zshaddhistory() {
[[ $1 != 'dc '* ]]
}
Run Code Online (Sandbox Code Playgroud)
我现在已经发布了一个更全面的解决方案作为插件:https://github.com/marlonrichert/zsh-hist