Adding timestamp to each line on Zsh
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:
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:
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.
Andre Cytryn
Updated on June 17, 2022Comments
-
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:
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 over 7 yearsI don't have .zsh file... should it be on .zshrc? (It didn't work anyway)
-
Michael Dautermann over 7 yearswhen 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 over 7 yearsI 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 over 7 yearspowerline-shell uses a
precmd
function to dynamically set the value ofPROMPT
prior to it being displayed. You need to follow the instructions at github.com/banga/powerline-shell to modify your prompt. -
Zulhilmi Zainudin about 5 yearsHow to put this on the right hand side of the screen?
-
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 over 4 yearsAwesome. Works as expected. Thanks.
-
Søren Pedersen almost 4 yearsIf you just want a 24 hour timestamp, you can use this
PROMPT='%{$fg[yellow]%}[%D{%T}] '$PROMPT
-
Hi-Angel over 3 yearsA bit of simplification: you can remove the middle
} %D{
part. So you'd havePROMPT='%{$fg[yellow]%}[%D{%f/%m/%y %L:%M:%S}] '$PROMPT
. Works for me this way. -
cdabel over 2 yearswhat if I just want unix timestamp prepended? How would I do that?