【问题标题】:Constantly updated clock in zsh prompt?zsh 提示符中不断更新时钟?
【发布时间】:2011-01-12 08:52:16
【问题描述】:

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

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

【问题讨论】:

    标签: zsh prompt zsh-zle


    【解决方案1】:

    注意:我为a similar question 写了这个答案,但是看到这个问题有更多的观点,我认为在这里重新发布我的答案会很有用。

    这实际上是可能的,无需借助奇怪的技巧。我的 .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 年!)

    【讨论】:

    • 我可以确认这确实有效,尽管这看起来很简单。您真正需要的只是$HOME/.zshrc 文件中的TMOUT=<SECONDS>; TRAPALRM () { zle reset-prompt } 部分,一切顺利。
    • 这应该是公认的答案。我还建议超时时间超过 1 秒。我用TMOUT=30。我注意到我的贝壳每秒钟都挂着。
    • 另外,请注意,这可能会使复制粘贴更烦人。
    • 将 Trapalrm BEFORE tmout 设置为在我的一个 Centos 盒子上执行空 TRAPALRM 会导致注销
    • 不喜欢这样,因为我无法再向后滚动并仔细阅读思考。当提示刷新时,它想滚动到底部
    【解决方案2】:

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

    幸运的是Peter Stephenson posted a technique。在 .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
    

    【讨论】:

    • 刚刚注意到帖子要求每秒更新一次。我还没有尝试过,但我猜在 schedprompt() 末尾的“sched +1 schedprompt”会做到这一点。
    • 原来对 reset-prompt 的调用会撤消术语窗口中的任何滚动。很难向后滚动并在那里停留任何时间。
    • 太棒了!它不仅显示当前时间,还显示执行命令的时间戳。关于滚动问题 - 您只需关闭您选择的终端的scroll on output 功能。
    • @ArtemIce 如果你只需要命令时间戳(就像我一样),你可以使用这样的东西: preexec () { echo -e "\033[1Adate +%H:%M:%S " }
    【解决方案3】:

    这在标准 zsh 提示符(或 bash 或其他 shell)中会令人不快。

    我建议你最好使用 Gnu Screen。

    屏幕可以有一个可以显示时间的状态行。 Here's an example screenrc 向下滚动到“Red Hat Magazine A guide to GNU 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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多