English 中文(简体)
Constantly updated clock in zsh prompt?
原标题:

I know that I can exec a date command in my zsh prompt. However, it shows the old time; to see the current time, I have to hit <return> and get a new prompt with the current time.

Is there a way to configure the zsh prompt to constantly update itself every second?

最佳回答

This would be .... unpleasant in a standard zsh prompt (or bash, or other shells).

I suggest you d be better off using Gnu Screen.

Screen can have a status line which can show the time. Here s an example screenrc scroll down to "Red Hat Magazine A guide to GNU Screen" to see the sample (i ll reproduce that here) which will, when screen is run, show the current time in the lower right corner of the terminal:

~/.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/

问题回答

Note: I wrote this answer for a similar question, but seeing how this question has more views I think reposting my answer here would be useful.

This is in fact possible without resorting to strange hacks. I ve got this in my .zshrc

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

TMOUT=1

TRAPALRM() {
    zle reset-prompt
}

The TRAPALRM function gets called every TMOUT seconds (in this case 1), and here it performs a prompt refresh, and does so until a command starts execution (and it doesn t interfere with anything you type on the prompt before hitting enter).

Source: http://www.zsh.org/mla/users/2007/msg00944.html (It s from 2007!)

Sounds like a pleasant request to me. If anything it makes more sense than showing the time when the prompt was displayed.

Fortunately Peter Stephenson posted a technique. Try something like this in .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 alias -> function?

Suppose I have: alias gg="git grep" then stuff like: gg "int x" works, but gg int x gets complaints. Is there a way to rewrite gg as a function in zsh so that it takes all the arguments after ...

zsh handling of arguments

In the zsh programming language, how do I get the entire list of arguments as a string in "" ? i.e. in myzshcommand 1 2 3 foo bar I want something to match me "1 2 3 foo bar" Thanks!

Tell if a user has SUed in a shell script?

I have a script which executes a git-pull when I log in. The problem is, if I su to a different user and preserve my environment with an su -lp, the script gets run again and usually gets messed up ...

How do I require a minimum version of zsh?

I want to use a feature only present in newer versions of zsh: [[ "$foo" =~ "regexp" ]] where regexp is a regular expression. Is it possible to do this check? Something like: if [[ $ZSH_VERSION &...

Rename files based on sorted creation date?

I have a directory filled with files with random names. I d like to be able to rename them file 1 file 2 etc based on chronological order, ie file creation date. I could be writing a short Python ...

ZSH Prompt/RPrompt Conflict

I currently have my ZSH PROMPT variable set up as: PROMPT=$ [%{e[0;32m%}%n@%m:%~%{e[0m%}]> and my RPROMPT variable as: RPROMPT=$ [%{e[0;32m%}%Te[0m%}] The effect I m working for is to make ...

热门标签