Adding timestamp to each line on Zsh

19,115

Solution 1

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

Solution 2

If you want it on the right side:

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

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

Solution 3

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

PROMPT='[%T] %n ~ %d %%'

Solution 4

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.
Share:
19,115
Andre Cytryn
Author by

Andre Cytryn

Updated on June 17, 2022

Comments

  • Andre Cytryn
    Andre Cytryn almost 2 years

    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
    Andre Cytryn over 7 years
    I don't have .zsh file... should it be on .zshrc? (It didn't work anyway)
  • Michael Dautermann
    Michael Dautermann over 7 years
    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?
  • Andre Cytryn
    Andre Cytryn over 7 years
    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
  • chepner
    chepner over 7 years
    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 github.com/banga/powerline-shell to modify your prompt.
  • Zulhilmi Zainudin
    Zulhilmi Zainudin about 5 years
    How to put this on the right hand side of the screen?
  • wcyn
    wcyn almost 5 years
    @ZulhilmiZainudin Looks like that could be quite a bit of work from this post: 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
  • Shiva Avula
    Shiva Avula over 4 years
    Awesome. Works as expected. Thanks.
  • Søren Pedersen
    Søren Pedersen almost 4 years
    If you just want a 24 hour timestamp, you can use this PROMPT='%{$fg[yellow]%}[%D{%T}] '$PROMPT
  • Hi-Angel
    Hi-Angel over 3 years
    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.
  • cdabel
    cdabel over 2 years
    what if I just want unix timestamp prepended? How would I do that?