我知道我可以date
在 zsh 提示符下执行命令。但是,它显示了旧时代;要查看当前时间,我必须点击<return>
并获得当前时间的新提示。
有没有办法将 zsh 提示配置为每秒不断更新自己?
注意:我为一个类似的问题写了这个答案,但是看到这个问题有更多的观点,我认为在这里重新发布我的答案会很有用。
这实际上是可能的,无需借助奇怪的技巧。我的 .zshrc 中有这个
RPROMPT='[%D{%L:%M:%S %p}]'
TMOUT=1
TRAPALRM() {
zle reset-prompt
}
TRAPALRM 函数每 TMOUT 秒调用一次(在本例中为 1),在这里它执行提示刷新,并一直执行到命令开始执行(并且它不会干扰您在按 Enter 之前在提示上键入的任何内容)。
资料来源:http ://www.zsh.org/mla/users/2007/msg00944.html (从 2007 年开始!)
听起来对我来说是一个愉快的要求。如果有的话,它比显示提示显示的时间更有意义。
幸运的是Peter Stephenson 发布了一项技术。在 .zshrc 中尝试这样的操作:
PROMPT="[%T] %n@%M %~ %# "
schedprompt() {
emulate -L zsh
zmodload -i zsh/sched
# Remove existing event, so that multiple calls to
# "schedprompt" work OK. (You could put one in precmd to push
# the timer 30 seconds into the future, for example.)
integer i=${"${(@)zsh_scheduled_events#*:*:}"[(I)schedprompt]}
(( i )) && sched -$i
# Test that zle is running before calling the widget (recommended
# to avoid error messages).
# Otherwise it updates on entry to zle, so there's no loss.
zle && zle reset-prompt
# This ensures we're not too far off the start of the minute
sched +30 schedprompt
}
schedprompt
这在标准的 zsh 提示符(或 bash 或其他 shell)中会令人不快。
我建议您最好使用 Gnu Screen。
屏幕可以有一个可以显示时间的状态行。 这是一个示例 screenrc向下滚动到“Red Hat Magazine A guide to GNU Screen”以查看示例(我将在此处重现该示例),当 screen 运行时,该示例将在终端的右下角显示当前时间:
~/.screenrc
hardstatus alwayslastline hardstatus 字符串 '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f% t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W }%c %{g}]' # 默认屏幕 屏幕-t shell1 0 屏幕-t shell2 1