0

I'm having difficulty with specifying PS1 on Ubuntu 18.04.5. I use the following code in my .bashrc file and the result looks exactly as it should.

export PS1=$'\e[38;5;046m \u00AB\D{%y-%m-%d}\u00BB-[\D{%H:%M:%S}]-{\W}\e[1m\e[38;5;196m>\e[38;5;226m>\e[38;5;046m>\e[38;5;255m\e[0m '

enter image description here

The problem is that my cursor is at the beginning of the command line and not after the >>>. Curiously, if I set my terminal to full screen, the cursor appears where it should. Is there an obvious error in my PS1 definition?

1
  • 1
    Just FYI, I'm on ubuntu 19.10 and it works as expected for me.
    – bashBedlam
    Dec 7, 2020 at 2:30

1 Answer 1

1

The problem is that it is over 80 characters long, and bash doesn't know fully how to parse the escape sequences. In particular, it thinks there is a line wrap going on, and it tries to produce it or compensate for it.

As for how to fix it? Put explicit newlines into your PS1.

I tried appending \e7\eM\n\e8 and it seemed to, mostly, work. In case you are wondering: \e7 is save position. \eM is "reverse index", basically a reverse linefeed. \n is a newline, and \e8 is restore position.

1
  • Thanks for the solution, David! This is a great workaround (+1). Wrapping non-printable characters in \[ and \] seems to prevent the problem in the first place, as described in the solution given above.
    – Dan
    Dec 7, 2020 at 13:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.