如何从ZSH中的历史记录中删除条目

时间:2011-08-30 13:31:14

标签: zsh

假设我使用zsh运行命令

echo "mysecret" > file

我可以使用命令fc -l轻松打印包含条目编号的历史记录:

1  echo "mysecret" >| file

但是如何轻松删除历史记录中的条目?

我在 man zshbuiltins 中找不到相应的段落。

5 个答案:

答案 0 :(得分:83)

* BSD /达尔文:

LC_ALL=C sed -i '' '/porn/d' $HISTFILE

Linux(GNU sed):

LC_ALL=C sed -i '/porn/d' $HISTFILE

这将从$ HISTFILE中删除所有与“porn”匹配的行。

使用setopt HIST_IGNORE_SPACE,您可以在上面的命令前加一个空格字符,以防止它被写入$ HISTFILE。

作为Tim pointed out in his comment below,前缀LC_ALL = C可防止“非法字节序列”失败。

答案 1 :(得分:41)

我不知道是否有一些优雅的方法可以做到这一点,但在类似的情况下我已经注销(允许zsh清空其缓冲区并将我的历史记录写入文件),然后登录,最后手动编辑 ~/.zsh_history,删除“危险”行。

答案 2 :(得分:24)

如果在zsh中使用HIST_IGNORE_SPACE选项,则可以使用空格“”前置命令,并且不会在历史记录文件中记住这些命令。如果你有秘密命令,你可以通常使用这些命令:

http://unsyncopated.com/blog/index.php/2007/08/18/omitting-certain-commands-from-zshs-history/

答案 3 :(得分:1)

此功能将从Zsh历史记录中删除您想要的任何一行,而不会出现任何问题:

# Accepts one history line number as argument.
# Alternatively, you can do `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."
}

如果要另外防止所有dc命令写入历史记录,请在~/.zshrc文件中添加以下内容:

zshaddhistory() {
 [[ $1 != 'dc '* ]]
}

更新

我现在作为插件发布了更全面的解决方案:https://github.com/marlonrichert/zsh-hist

答案 4 :(得分:-2)

bash 中:

1-在bash终端类型中

hive> select len("some string"); OK 11 Time taken: 1.758 seconds, Fetched: 1 row(s) #这将列出历史记录.bash_history文件中具有行号的所有命令

例如:

hsitory

2-选择要删除的CMD行号

... 987 cd 988 ssh x@127.0.0.1 990 exit 991 cd

注意:例如,如果要删除最后3个CMD,只需从底部ex 988中选择第三行号,然后依次重复CMD history -d 988 3次。

Source

相关问题