46

I want to add a timestamp to my command prompt in oh-my-zsh. This is the current theme prompt (robbyrussell):

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ %s)"
PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'

ZSH_THEME_GIT_PROMPT_PREFIX="git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

Any idea how can add the timestamp?

5 Answers 5

59

According to the zshmisc man page there are several % codes for date and time, eg:

 %D     The date in yy-mm-dd format.
 %T     Current time of day, in 24-hour format.
 %t %@  Current time of day, in 12-hour, am/pm format.
 %*     Current time of day in 24-hour format, with seconds.
 %w     The date in day-dd format.
 %W     The date in mm/dd/yy format.
 %D{strftime-format}

The last one allows codes listed in the strftime(3) man page. Edit your ~/.zshrc file and add at the end a new PROMPT value, eg:

 PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} %D %T % %{$reset_color%}'
2
  • This doesn't stay after I close the terminal window. I have to do source ~/.zsh every time how do I avoid that?
    – user391339
    Oct 24, 2019 at 22:59
  • 2
    There was a typing mistake in my answer. The file to use is ~/.zshrc not ~/.zsh.
    – meuh
    Oct 25, 2019 at 6:07
41

If you want add date/time to the right, you set RPROMPT

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
RPROMPT="[%D{%y/%m/%f}|%@]"

enter image description here

4
  • 2
    its definitely goes better on the right side since I already have a lot of stuff on the left prompt
    – WhoSayIn
    Mar 9, 2018 at 13:30
  • 2
    in my case nano ~/.zshrc and added RPROMPT="%{$fg[yellow]%}[%D{%f/%m/%y}|%@]" as last line to set right prompt with yellow color Jun 25, 2019 at 17:32
  • 2
    @VictorR.Oliveira you would want to add %{$reset_color%} at the end to avoid having the yellow messing up the rest of the lines.
    – Raf
    Nov 24, 2019 at 18:22
  • 2
    +1 for RPROMPT
    – ThisaruG
    Mar 4, 2020 at 7:47
11

I added this %D{%m/%f/%y}|%D{%L:%M:%S} to the main theme to display the date and time as mm/dd/yy | hh:mm:ss.

So here is the full command I use:

PROMPT='%D{%m/%f/%y}|%D{%L:%M:%S} ${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}${NEWLINE}$ '

Which gives:

enter image description here

3

To get full datetime and zone

[Sun 11 Aug 2019 20:41:53 AEST]

I place

RPROMPT="%{$fg[green]%}[%D{%c}]"

in my ~/.zshrc file

1

This is what worked for me

%D{%b %d, %Y - %T}

Format: Aug 10, 2020 - 22:26:54

Reference: https://man7.org/linux/man-pages/man1/date.1.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.