You are not logged in.

#1 2016-01-29 08:10:24

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

how to update command prompt timestamp ?

To show timestamp in command prompt at terminal startup.
something like in ~/.bashrc

PS1='[\u@\h \W] \T $ '

just Enter to update that timestamp.

but how can I automatically update that timestamp?

I tried like this but not work.

PS1='[\u@\h \W]  $(while true; do \T sleep 1 done &)  $ '

any idea?

Last edited by duyinthee (2016-01-29 08:14:28)

Offline

#2 2016-01-29 09:00:20

Ziusudra
Member
Registered: 2014-04-19
Posts: 120

Re: how to update command prompt timestamp ?

Are you trying to have the time update on your current prompt? I don't know if that's even possible ... or wise.

https://wiki.archlinux.org/index.php/Ba … tomization

Offline

#3 2016-01-29 09:48:51

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: how to update command prompt timestamp ?

I don't think bash can do that. There is PROMPT_COMMAND, but that's only for setting a function that is executed whenever the prompt is (newly) displayed.

But zsh can do it - I found this zsh config snippet in my dotfiles' git history:

setopt prompt_subst
TMOUT=1
TRAPALRM() {
    zle reset-prompt
}
PROMPT='%D{%H:%M:%S}'

However, I agree with Ziusudra: it's not very wise.

There is a reason why it's in my dotfile's git history: after the initial excitement about a clock in the prompt (yay?), I started getting annoyed about the used space and the occasional glitches (especially at terminal resize events). Also, it feels a little silly, given that my WM is perfectly able to display the time on its own.

If, for some reason, you really need a clock in your shell, have you taken a look at tmux? It is capable of displaying the current time in the status bar, which keeps the amount of stuff in your shell prompt to a minimum (on the other hand, it's a terminal multiplexer, which comes with its own set of benefits/problems).

Last edited by ayekat (2016-01-29 09:49:18)


pkgshackscfgblag

Offline

#4 2016-01-29 13:57:33

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: how to update command prompt timestamp ?

ayekat wrote:

after the initial excitement about a clock in the prompt (yay?), I started getting annoyed about the used space and the occasional glitches (especially at terminal resize events). Also, it feels a little silly, given that my WM is perfectly able to display the time on its own.

I love having a timestamp in my prompt! It gives me a general idea of when a command was run and how long it took to finish. I kind of wish there was a way to get that information into my Bash history too! I've never had it glitch. And I use the extra space it takes up to easily visually separate the commands I type:

8:15:24 am drcouzelis@archsystem
[ Documents ]$ cat blarg.txt
Blarg.
Blarg?
BLARG!

8:15:44 am drcouzelis@archsystem
[ Documents ]$ 

Just posting another opinion on the matter. smile

Offline

#5 2016-01-29 18:23:28

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: how to update command prompt timestamp ?

That isn't the same as the prompt dynamically updating, though; which seems to be what the OP is after. And given the utility of that would only be if you sat staring at a blank terminal...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2016-01-29 18:53:30

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: how to update command prompt timestamp ?

jasonwryan wrote:

That isn't the same as the prompt dynamically updating, though; which seems to be what the OP is after. And given the utility of that would only be if you sat staring at a blank terminal...

That's true. On a mildly related note, I wonder if it's time for me to try switching to zsh... smile

Offline

#7 2016-01-29 21:01:54

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: how to update command prompt timestamp ?

@drcouzelis, I do have configured zsh to display the running time of the last command in the RPROMPT if the time > 1 second (so it wouldn't pop up for a simple `ls` or `cd`):

preexec() {
    timer=${timer:-$SECONDS}
}

precmd() {
    RPROMPT=''
    if [ -n "$timer" ]; then
        seconds=$(($SECONDS - $timer))
        if [ $seconds -gt 1 ]; then
            RPROMPT="$seconds"
        fi
        unset timer
        unset seconds
    fi
    export RPROMPT
}

setopt promptsubst
precmd

Of course this was the thing I did right after I realised a clock in the prompt is overkill for my needs, whereas this does exactly what I want: give me an idea of how long commands take to run. With a little more accurate styling I get something like this:

rprompt.png

drcouzelis wrote:

On a mildly related note, I wonder if it's time for me to try switching to zsh... smile

Yes smile


pkgshackscfgblag

Offline

#8 2016-01-29 21:19:02

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: how to update command prompt timestamp ?

@ayekat: Does it update while the command is running, or just print after the command completes?


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#9 2016-01-30 01:11:18

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

Re: how to update command prompt timestamp ?

Actually, it is useful to have update time and battery status at current prompt when we working with awfully minimalist WMs like EvilWM.
Bash can't do that but bash can do $(acpi) at least. So, at last I do that:

PS1='\n\D {%F %T} | $(acpi)\n[\u@\h \W] \$ '

just Enter to update it.
It is useful for me.
Thanks for replies.

Offline

#10 2016-01-30 07:18:37

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: how to update command prompt timestamp ?

@alphaniner, It just prints the running time of the last executed command in the next prompt. So yes, I can't see the running time during the execution. hmm
However that doesn't bother me too much. I can run `ps` to see the start time of a running process (which is not possible for already completed processes, hence this prompt).

@duyinthee, I would definitely suggest using tmux.


pkgshackscfgblag

Offline

Board footer

Powered by FluxBB