Home > Blockchain >  How to strip leading zeros/spaces in datetime format for *WINDOWS* git bash prompt?
How to strip leading zeros/spaces in datetime format for *WINDOWS* git bash prompt?

Time:10-09

This prompt works for my terminals in Ubuntu:

PS1="[\D{%-m/%-d %-l:%M:%S %P}] \u@\h:\w$ "

But in my git bash terminal in Windows VSCode, the datetime portion of that prompt breaks -- it shows up as an empty string. It doesn't like the "-" in the format specifiers. Is there a another way to do this? For example, a date like May 9th should appear like this: [5/9 1:23:07 pm], not like this [05/09 01:23:07 pm] or this [ 5/09 1:23:07 pm]. Since the prompt appears above my commands, lining up the datetime fields with padding is not important to me.

Note: %#m doesn't work, either, despite this strftime documentation from Microsoft.

In case it's relevant, here's the full (broken) script for my whole prompt:

PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]'  # set window title
PS1="$PS1"'\n'                          # new line
PS1="$PS1"'\[\033[32m\]'                # change to green
PS1="$PS1"'[\D{%-m/%-d %-l:%M:%S %P}] ' # date & time
PS1="$PS1"'\[\033[33m\]'                # change to brownish yellow
PS1="$PS1"'\w'                          # current working directory

CodePudding user response:

I think maybe using the date command with the PS1 prompt would help you get the desired result. Please find the below shell substitution,

PS1="$(date  '%-m/%-d %-l:%M:%S %P') $"

This above one does work with my git bash prompt. And I hope this is helpful.

  • Related