Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 759
Andre Cytryn Avatar asked Oct 16 '16 23:10

Andre Cytryn


People also ask

How do I add time to zsh?

Add the Date and Time to the zsh Prompt If you want to include the system time in the zsh prompt, add %T for the current time in 24-hour format, %t for the time to appear in AM/PM or 12-hour format, or use %* to display the time in the 24-hour format along with seconds.

Does zsh use PS1?

zsh uses the same shell variable PS1 to store the default prompt.

How do I change the prompt in oh my zsh?

To customize the ZSH prompt, we need to modify the prompt= variable inside the . zshrc file. We can populate the prompt variable with various placeholders, which will alter how the ZSH prompt appears.


1 Answers

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 
like image 103
wcyn Avatar answered Sep 22 '22 05:09

wcyn