Adding timestamp to each line on Zsh
Asked Answered
B

6

62

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?

Bellona answered 16/10, 2016 at 23:21 Comment(0)
F
116

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. You may refer to the official Zsh Date and Time formatting linked here: https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html#Date-and-time. This uses Linux's strftime3 (https://man7.org/linux/man-pages/man3/strftime.3.html) to format the Date (%D) string.

Make sure to reload your .zshrc file by typing:

. ~/.zshrc

or

source ~/.zshrc
Frohne answered 19/1, 2018 at 12:39 Comment(8)
How to put this on the right hand side of the screen?Hat
If you just want a 24 hour timestamp, you can use this PROMPT='%{$fg[yellow]%}[%D{%T}] '$PROMPTNapalm
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.Lambrecht
what if I just want unix timestamp prepended? How would I do that?Pfeiffer
@Frohne How can I add time stamp to right side and took time at the end of left side things?Spree
fun fact ab this one. if you ever resource your .zshrc your prompt just grows and growsssAsceticism
@Spree have a look at @ehacinom's answer below: https://mcmap.net/q/320044/-adding-timestamp-to-each-line-on-zsh Essentially, you need to use RPROMPT instead of PROMPTFrohne
Does anyone know how to use it with Pure? I can't get it workCespitose
Z
28

If you want it on the right side:

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

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

Zeena answered 25/3, 2020 at 2:52 Comment(1)
how can I add name of day to this command? and the takcing time to the end of left sideSpree
E
20

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.
Eldridge answered 13/9, 2021 at 14:19 Comment(0)
H
4

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

PROMPT='[%T] %n ~ %d %%'
Headrace answered 16/10, 2016 at 23:55 Comment(4)
I don't have .zsh file... should it be on .zshrc? (It didn't work anyway)Bellona
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?Headrace
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 outputBellona
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.Ossiferous
M
2

enter image description here

PROMPT='%F{219}[%D{%d/%m/%y %H:%M:%S}]%f '$PROMPT

Thanks to @wcyn for his great answer. I made it more readable by:

  1. Removing the second D
  2. Changing the hours format to 24-hour format: from L to H
  3. Changing the day format to 2 digits: from f to d
  4. Changing the coloring format

Explanation

To color text, use this syntax:

%F{color_code}My_Text%f

Specifically, color code 219 sets a light purple color. You can discover the color encodings with this awesome answer

Moorwort answered 4/9, 2023 at 8:56 Comment(0)
A
0

If you prefer the 24-hour format, use this

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

diff is '%L' to 'K'

Ansela answered 19/6 at 7:45 Comment(1)
take a look, zsh.sourceforge.io/Doc/Release/Prompt-Expansion.htmlAnsela

© 2022 - 2024 — McMap. All rights reserved.