0

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
Word Demon
  • 73
  • 2
  • 8
  • what shell do you use on Ubuntu? and have these the same version if it is bash? – rioV8 Oct 08 '21 at 15:28
  • Indeed, if I take out all the minuses from your prompt, it works in my Git Bash: `export PS1="[\D{%m/%d %l:%M:%S %P}] \u@\h:\w$ "`, but with 0-padded numbers, so it seems you're right, the `-` is a problem. – joanis Oct 08 '21 at 15:37
  • My guess is that the version of `strftime()` on Git-Bash just doesn't support that `-` syntax. I'm not sure there's a work-around for it. Can you live with the 0 padding? – joanis Oct 08 '21 at 15:38
  • @joanis lol, yes, I think I'll be able to survive the ordeal. Doesn't hurt to ask, though. – Word Demon Oct 08 '21 at 15:45
  • @rioV8 It's whatever their default is -- presumably bash. It's kind of irrelevant to my question, though. I just want a format string that will actually work in the default Windows VSCode terminal. – Word Demon Oct 08 '21 at 15:49
  • there is no default VSCode terminal, it all depends on which version of bash is running in in the git-bash shell, and that is not installed by VSC but by the user – rioV8 Oct 08 '21 at 15:53
  • 1
    @joanis kite came up with a working solution and spared me the horror of living with 0-padding in VSCode terminals. – Word Demon Oct 08 '21 at 16:26

1 Answers1

1

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.

kite
  • 441
  • 3
  • 8
  • 1
    No, I thought "$" needs to be escaped in bash terminal but I guess it is not required as the "$" at the end is not escaped. I will update the answer. – kite Oct 08 '21 at 16:17
  • Your initial code with the escaped `$` was correct! Took me a while to realize it, but if you don't escape it, then it only generates the prompt when you first open the terminal, and it doesn't update with each subsequent prompt. Turns out, the _real_ cause of my syntax error was some bizarre `\n` parsing issue at the bottom of my script, which I eventually managed to [solve](https://stackoverflow.com/a/69668675/10818186). So yeah, please revert your answer to your original solution. Thanks. :) – Word Demon Oct 21 '21 at 21:12