Andre Cytryn
Andre Cytryn

Reputation: 2705

Adding timestamp to each line on Zsh

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?

Upvotes: 67

Views: 47558

Answers (6)

Baker Kim
Baker Kim

Reputation: 1

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'

Upvotes: 0

wcyn
wcyn

Reputation: 4216

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

Upvotes: 124

AlonBA
AlonBA

Reputation: 505

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

Upvotes: 4

Valery Kovalev
Valery Kovalev

Reputation: 371

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.

Upvotes: 22

ehacinom
ehacinom

Reputation: 8894

If you want it on the right side:

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

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

Upvotes: 28

Michael Dautermann
Michael Dautermann

Reputation: 89509

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

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

Upvotes: 4

Related Questions