我知道可以在zsh提示符中执行date
命令。但是,它显示的是旧时间;要查看当前时间,必须按下<return>
并获取带有当前时间的新提示符。
有没有一种方法可以配置zsh提示符,使其每秒钟自动更新?
注意:我为一个类似的问题写了这个答案,但是看到这个问题有更多的浏览量,我认为在这里重新发布我的答案会很有用。
事实上,这是可以做到的,而不必诉诸奇怪的技巧。我在我的.zshrc文件中实现了这个功能。
RPROMPT='[%D{%L:%M:%S %p}]'
TMOUT=1
TRAPALRM() {
zle reset-prompt
}
TRAPALRM函数每隔TMOUT秒(在本例中为1)被调用一次,在此执行提示刷新,直到命令开始执行(并且不会干扰您在按回车键之前在提示符上输入的任何内容)。
来源:http://www.zsh.org/mla/users/2007/msg00944.html(来自2007年!)
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
date +%H:%M:%S
" }。请注意,我的翻译尽可能保留了原文的意思和结构,并尽力使它更容易理解。 - Dan Berindei在标准的zsh提示符(或bash、其他shell)中,这将会很不舒服。
我建议你最好使用Gnu Screen。
Screen可以有一个状态行,可以显示时间。 这里是一个screenrc示例,滚动到“Red Hat Magazine A guide to GNU Screen”以查看示例(我将在此处重现),当运行screen时,它将在终端的右下角显示当前时间:
~/.screenrc
hardstatus alwayslastline hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
# 默认屏幕 screen -t shell1 0 screen -t shell2 1
$HOME/.zshrc
文件中加入TMOUT=<SECONDS>; TRAPALRM () { zle reset-prompt }
这一部分,然后就可以使用了。 - Alexej MaguraTMOUT=30
。当超时时间只设为每秒一次时,我发现我的shell会挂起。 - brock