我看到了选项 Extended_history 并看到它的使用方式如下zsh 中每个目录的历史记录但还没有真正理解extend_history实际上是做什么的?
什么信息。将在 zsh_history 中可用,否则如果未设置此选项则不会。
从http://zsh.sourceforge.net/Doc/Release/Options.html#Options它说的是——
EXTENDED_HISTORY <C>
Save each command’s beginning timestamp (in seconds since the epoch) and the duration (in seconds) to the history file. The format of this prefixed data is:
‘: <beginning time>:<elapsed seconds>;<command>’.
但至少在运行历史命令时我没有看到它有任何好处 -
shirish@debian ~ % history | grep tail -5
601* exit
602* history
603* exit
604 cd
605 cat ~/.zsh/.zshrc
606 tail -5 history
这就是我的~/.zsh/.zshrc
设置方式 -
shirish@debian ~ % cat ~/.zsh/.zshrc
# Lines configured by zsh-newuser-install
HISTFILE=~/.zsh//.histfile
HISTSIZE=1000
SAVEHIST=100000
setopt inc_append_history autocd nomatch notify share_history extended_history
bindkey -e
# End of lines configured by zsh-newuser-install
# display how long all tasks over 10 seconds take
export REPORTTIME=10
# The following lines were added by compinstall
zstyle :compinstall filename '/home/shirish/.zsh//.zshrc'
autoload -Uz compinit promptinit
compinit
promptinit
# End of lines added by compinstall
prompt adam1
设置选项或删除选项后我没有看到任何差异,没有任何有意义的差异。
shirish@debian ~ % cat ~/.zsh/.zshrc | grep setopt
setopt inc_append_history autocd nomatch notify share_history extended_history
shirish@debian ~ % source ~/.zsh/.zshrc
shirish@debian ~ % history -E
337 11.1.2018 23:33 history | grep apg
338 11.1.2018 23:33 man apg
339 11.1.2018 23:33 cd
340 11.1.2018 23:33 apg -a 1 -M n -n 3 -m 20
341 11.1.2018 23:33 apg -a 1 -M SNCL -n 3 -m 20
342 11.1.2018 23:33 history
343 11.1.2018 23:33 history -E
344 11.1.2018 23:33 leafpad ~/.zsh/.zshrc
345 11.1.2018 23:33 cat ~/.zsh/.zshrc
346 11.1.2018 23:33 source ~/.zsh/.zshrc
347 11.1.2018 23:33 history -E
348 11.1.2018 23:33 exit
349 11.1.2018 23:33 history -E
350 11.1.2018 23:34 leafpad ~/.zsh/.zshrc
351 11.1.2018 23:35 cat ~/.zsh/.zshrc | grep setopt
352 11.1.2018 23:35 source ~/.zsh/.zshrc
shirish@debian ~ % leafpad ~/.zsh/.zshrc
shirish@debian ~ % source ~/.zsh/.zshrc
shirish@debian ~ % history -E
340 11.1.2018 23:33 apg -a 1 -M n -n 3 -m 20
341 11.1.2018 23:33 apg -a 1 -M SNCL -n 3 -m 20
342 11.1.2018 23:33 history
343 11.1.2018 23:33 history -E
344 11.1.2018 23:33 leafpad ~/.zsh/.zshrc
345 11.1.2018 23:33 cat ~/.zsh/.zshrc
346 11.1.2018 23:33 source ~/.zsh/.zshrc
347 11.1.2018 23:33 history -E
348 11.1.2018 23:33 exit
349 11.1.2018 23:33 history -E
350 11.1.2018 23:34 leafpad ~/.zsh/.zshrc
351 11.1.2018 23:35 cat ~/.zsh/.zshrc | grep setopt
352 11.1.2018 23:35 source ~/.zsh/.zshrc
353 11.1.2018 23:35 history -E
354 11.1.2018 23:36 leafpad ~/.zsh/.zshrc
355 11.1.2018 23:36 source ~/.zsh/.zshrc
shirish@debian ~ % cat ~/.zsh/.zshrc | grep setopt
setopt inc_append_history autocd nomatch notify share_history
可以看出,似乎没有任何区别。
% zsh --version
zsh 5.4.2 (x86_64-debian-linux-gnu)
答案1
无论选项是什么, 的输出history -E
总是相同的EXTENDED_HISTORY
。命令的持续时间直接存储在历史文件中(只需检查文件即可查看值)。
然而,另一个问题是有一些选项可以覆盖此行为。您可以通过运行类似的命令来测试它是否正常工作sleep 3
,这应该会产生如下所示的条目:
pol@host ~ $ sleep 3
pol@host ~ $ tail -1 ${HISTFILE}
: 1530663493:3;sleep 3
pol@host ~ $ setopt | grep hist
extendedhistory
histignoredups
incappendhistorytime
您可以看到“持续时间”值为 3。如果它不是 3,则很可能您设置了另一个阻止EXTENDED_HISTORY
工作的选项。这些包括SHARED_HISTORY
和INC_APPEND_HISTORY
。如果您需要前者,那么您就不走运了。对于后一种情况,INC_APPEND_HISTORY_TIME
如果您还想拥有EXTENDED_HISTORY
值(如我上面所述),则可以使用另一种替代方法。
答案2
我会注意到这个答案https://unix.stackexchange.com/a/453336/277040有点误导。确实,输出格式无论状态如何,都history -E
将相同,但如果Zsh 会话始终如此,则EXTENDED_HISTORY
时间戳将相对毫无意义。unsetopt EXTENDED_HISTORY
的每一行都HISTFILE
将包含一个没有任何时间戳信息的命令,因此history -E
所有这些命令都会输出相同的时间(打开特定 shell 并将其HISTFILE
读入内存的时间)。
例如,给定~/.zsh_history
包含过去不同时间执行的命令:
echo 1
echo 3
echo 5
当我在写这个答案的那天下午 5:03 打开 shell 时,history -E
输出:
1 24.6.2020 17:03 echo 1
2 24.6.2020 17:03 echo 3
3 24.6.2020 17:03 echo 5