51

I just fresh installed Sierra and wanted to use zsh with oh-my-zsh and power shell...

I ended up with a terminal like this:

enter image description here

But I want to add a timestamp to every output. Semething linke:

[14:23] acytryn ~ Projects %

Is there a way to do this with zsh?

Andre Cytryn
  • 2,506
  • 4
  • 28
  • 43

4 Answers4

100

I've found it more non-destructive to actually prepend the time to the existing prompt without overriding it completely. This makes it work with any existing theme without interfering with its styling.

Add this at the end of your .zshrc file. You can type the command nano ~/.zshrc to edit it using nano:

PROMPT='%{$fg[yellow]%}[%D{%f/%m/%y} %D{%L:%M:%S}] '$PROMPT

I use cloud theme, so this gives me:

enter image description here

It retains the current theme. You can also add some styling to the timestamp, by changing the color, or even the format.

Make sure to reload your .zshrc file by typing:

. ~/.zshrc

or

source ~/.zshrc
wcyn
  • 3,826
  • 2
  • 31
  • 25
  • How to put this on the right hand side of the screen? – Zulhilmi Zainudin Apr 17 '19 at 19:06
  • @ZulhilmiZainudin Looks like that could be quite a bit of work from this post: https://superuser.com/questions/187455/right-align-part-of-prompt. You can try something from the answers there and see if it works for you. I haven't attempted any of it though – wcyn Jul 16 '19 at 12:59
  • 7
    If you just want a 24 hour timestamp, you can use this `PROMPT='%{$fg[yellow]%}[%D{%T}] '$PROMPT` – Søren Pedersen May 28 '20 at 15:01
  • 2
    A bit of simplification: you can remove the middle `} %D{` part. So you'd have `PROMPT='%{$fg[yellow]%}[%D{%f/%m/%y %L:%M:%S}] '$PROMPT`. Works for me this way. – Hi-Angel Nov 18 '20 at 12:41
  • what if I just want unix timestamp prepended? How would I do that? – cdabel Sep 07 '21 at 18:51
  • @wcyn How can I add time stamp to right side and took time at the end of left side things? – Farshad Jul 21 '22 at 07:14
  • fun fact ab this one. if you ever resource your .zshrc your prompt just grows and growsss – Patrick Michaelsen Jul 22 '22 at 12:34
25

If you want it on the right side:

RPROMPT="[%D{%f/%m/%y} | %D{%L:%M:%S}]"

https://gist.github.com/zulhfreelancer/9c410cad5efa9c5f7c74cd0849765865

ehacinom
  • 8,070
  • 7
  • 43
  • 65
18

Yes. Just open your ~/.zshrc and add this line at the end of it (using nano ~/.zshrc command in terminal, for example):

PROMPT='%{$fg[yellow]%}[%*] '$PROMPT

And you'll get it like this:

enter image description here

You can change [%*] section to get other formats:

 %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.
Valery Kovalev
  • 331
  • 2
  • 6
3

add this to the bottom of your ~/.zsh file:

PROMPT='[%T] %n ~ %d %%'
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • 1
    I don't have .zsh file... should it be on .zshrc? (It didn't work anyway) – Andre Cytryn Oct 17 '16 at 13:55
  • when you say "it didn't work anyway", what didn't work? putting it in the .zshrc or even typing "`PROMPT='[%T] %n ~ %d %%'`" into your zsh shell to try it out? – Michael Dautermann Oct 17 '16 at 14:34
  • I added to the last file of .zshrc file, runned `source ~/.zshrc` but nothing changed. I am using powerline-shell, I dont know if that interferes somehow. Also, pasting that into prompt directly has no output – Andre Cytryn Oct 17 '16 at 15:14
  • 1
    powerline-shell uses a `precmd` function to dynamically set the value of `PROMPT` prior to it being displayed. You need to follow the instructions at https://github.com/banga/powerline-shell to modify your prompt. – chepner Oct 17 '16 at 17:02