我在一个拥有大量需要大量选项的工具的地方工作,所以我非常依赖我的 shell 的历史。我什至不时地备份它,以确保我不会丢失有用的、冗长的命令。
我刚刚输入了这些命令之一,我想确保它已刷新到历史文件中,但我在后台有一个长期运行的作业,我无法输入exec zsh
. 在这种情况下我还能做些什么吗?
(当然,我可以将它复制并粘贴到一个文件中,但存在一个flush-history
命令会更合乎逻辑。)
要将 shell 历史记录写入历史文件,请执行
fc -W
fc
有一些有用的标志,在man zshbuiltins
.
您还可以在每个命令之后完全自动读取和写入历史文件(从而与每个正在运行的 zsh 自动共享您的历史文件),方法是setopt -o sharehistory
. 在 中阅读更多与历史相关的选项man zshoptions
。
我也刚刚发现:
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).
并使用
fc -R
在现有的 zsh shell 中读取历史记录(在写入之后)。
将下面的行添加到 ~/.zshrc,它将保存 1000 个我们通过更改 HISTSIZE 和 SAVEHIST 的值来增加的条目
HISTSIZE=1000
if (( ! EUID )); then
HISTFILE=~/.zsh_history_root
else
HISTFILE=~/.zsh_history
fi
SAVEHIST=1000