oh-my-zsh 历史记录日期格式化

5
我试图格式化 oh-my-zsh 的历史记录输出。 看起来应该使用 fc 命令,因为 history 忽略了 HISTFILEHISTSIZEHISTTIMEFORMAT 中的任何设置。
有没有一种方法可以像使用 date 一样格式化它呢?
我找不到关于 fc 命令的合适文档。也没有关于 fc 的手册页面。
目前有效的是使用 fc -flE 输出带有欧洲格式的历史记录。
最终,我的目标是找出我今天键入了多少个命令以及何时键入的。
这里有一个小问题。
目前我有这个设置: alias today='date +"%d.%m.%Y"' 没有格式标记可以让我删除前导零(我想要6而不是06)。
但是我在这里有更多的格式选项,而用奇怪的 zsh 历史命令 fc 就不行。 历史记录将日期输出为 22.6.2016,而 today 是 22.06.2016 因此,我无法执行以下操作: fc -flE | grep $(today) | wc -l 非常感谢您的任何反馈!
1个回答

5

简短回答:

fc -li | grep $(date -I)

选项-i指示fc以ISO 8601 yyyy-mm-dd HH:MM格式打印时间戳。选项-I指示日期以ISO 8601格式(不带时间)输出日期。


至于您遇到的问题:

  1. As fc is a builtin of zsh you can find its documentation in the zshbuiltin manpage.

  2. The options -f (print time stamps in US MM/DD/YY hh:mm format) and -E (print time stamps in the European dd.mm.yyyy hh:mm format) are mutually exclusive, with -E taking precedence (silently, without error message). If you do not want the European format, drop the -E option.

  3. You can explicitly specify the time stamp format for fc with the option -t fmt. The following will print time stamps like 06/22/2016 - 23:07:09 (note the zero padding):

    fc -lt '%m/%d/%Y - %H:%M:%S'
    

    This uses the strftime function (more info with man 3 strftime) to format the time stamp. So the format options are nearly identical to those you can use with date.

  4. You can tell date not to pad a field by using the - flag. For example 6/22/16:

    date +%-m/%-d/%y
    

    This flag also works for fc.


稍微长一点的答案:

如果你喜欢 ISO 8601 标准以外的时间戳格式,你可以指定一个并在 datefc 中使用它。例如:非填充的美国日期格式和带有秒钟的 0 填充时钟时间:

TS_DATE="%-m/%-d/%y"
TS_TIME="%H:%M:%S"
fc -lt "$TS_DATE $TS_TIME" | grep $(date +$TS_DATE)

非常感谢!这个命令的信息真的很难找到。 感谢您的时间和努力。我非常感激 :) - Martin Muzatko
1
我已经使用oh-my-zsh几年了,我需要获取最新的.zshrc模板(https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/templates/zshrc.zsh-template)以获取HIST_STAMPS选项。也许其他人会遇到同样的问题。 - Randy L

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