Add timestamp to oh-my-zsh robbyrussell theme
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?
Solution 1:
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%}'
Solution 2:
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}|%@]"
Solution 3:
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:
Solution 4:
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