如何告诉Zsh将当前Shell的历史记录写入我的历史记录文件?

28

我在一个需要大量选项的工具中工作,因此我非常依赖于我的shell历史记录。我甚至会定期备份它,以确保我不会失去有用的长命令。

我刚刚输入了其中一个命令,并希望确保它被刷新到历史文件中,但我有一个正在后台运行的长时间任务,不能键入exec zsh。在这种情况下,我还能做些什么吗?

(当然,我可以将其复制并粘贴到文件中,但存在一个flush-history命令会更有逻辑性。)

4个回答

49
写入Shell历史记录到历史文件,执行以下命令:
fc -W

fc 有一些有用的标志,可以在 man zshbuiltins 中查看所有标志。

你也可以通过输入 setopt -o sharehistory 来完全自动化地读取和写入每个命令后的历史文件(因此自动与运行中的每个 zsh 共享您的历史文件)。在 man zshoptions 中阅读更多关于历史记录的选项。


1
Documentation: http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html#Shell-Builtin-Commands - The Demz

23

我也刚刚发现:

setopt INC_APPEND_HISTORY

来自于 man zshoptions:

INC_APPEND_HISTORY
   This  options  works like APPEND_HISTORY except that new history
   lines are added to the $HISTFILE incrementally (as soon as  they
   are  entered),  rather  than waiting until the shell exits.  The
   file will still be periodically re-written to trim it  when  the
   number  of  lines grows 20% beyond the value specified by $SAVE-
   HIST (see also the HIST_SAVE_BY_COPY option).

16

然后使用

fc -R

如何在已有的 zsh shell 中读取历史记录(写入后)。


2
将以下内容添加到~/.zshrc中,这将保存我们通过更改HISTSIZE和SAVEHIST值增加的1000个条目。
HISTSIZE=1000
if (( ! EUID )); then
  HISTFILE=~/.zsh_history_root
else
  HISTFILE=~/.zsh_history
fi
SAVEHIST=1000

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接