在zsh提示符中不断更新时钟?

ano*_*non 8 zsh prompt zsh-zle

我知道我可以date在zsh提示符下执行命令.然而,它显示了时代; 要查看当前时间,我必须按当前时间点击<return>并获得新提示.

有没有办法配置zsh提示每秒不断更新自己?

nit*_*han 39

注意:我为类似的问题写了这个答案,但是看看这个问题如何有更多的观点,我认为在这里重新发布我的答案会很有用.

事实上,这可能不会诉诸于奇怪的黑客.我的.zshrc中有这个

RPROMPT='[%D{%L:%M:%S %p}]'

TMOUT=1

TRAPALRM() {
    zle reset-prompt
}
Run Code Online (Sandbox Code Playgroud)

TRAPALRM函数每TMOUT秒调用一次(在本例中为1),此处执行快速刷新,直到命令开始执行为止(并且它不会干扰您在按Enter键之前在提示符上键入的任何内容).

资料来源:http://www.zsh.org/mla/users/2007/msg00944.html(从2007年开始!)

  • 这应该是公认的答案.我还建议超过1秒的超时.我用'TMOUT = 30`.我注意到我的外壳每秒都悬挂着. (3认同)
  • 另请注意,这可能会使复制粘贴变得更烦人。 (2认同)

小智 8

这将是......在标准的zsh提示符(或bash或其他shell)中令人不快.

我建议你最好使用Gnu Screen.

屏幕可以有一个状态行,可以显示时间. 这是一个示例screenrc向下滚动到"红帽杂志GNU屏幕指南"以查看示例(我将在此处重现),当屏幕运行时,将显示终端右下角的当前时间:

〜/ .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}]'

# Default screens
screen -t shell1        0
screen -t shell2        1

http://www.gnu.org/software/screen/


Dav*_*hen 7

听起来对我来说是一个愉快的要求.如果有什么比显示提示显示的时间更有意义.

幸运的是,彼得斯蒂芬森发布了一项技术.在.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
Run Code Online (Sandbox Code Playgroud)

  • @ArtemIce如果您只需要命令时间戳(就像我一样),您可以使用以下内容:preexec(){echo -e"\ 033 [1A`date +%H:%M:%S`"} (4认同)