24

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?

36

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 you ~/.zsh 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%}'
18

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

  • 2
    its definitely goes better on the right side since I already have a lot of stuff on the left prompt – WhoSayIn Mar 9 '18 at 13:30
  • in my case nano ~/.zshrc and added RPROMPT="%{$fg[yellow]%}[%D{%f/%m/%y}|%@]" as last line to set right prompt with yellow color – Victor R. Oliveira Jun 25 at 17:32
7

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

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.