How to properly set up PS1 if I want \n there?

PS1="\[\033[38;5;132m\]\n┌─■ testing\n└─■ \[$(tput sgr0)\]"

Currently I have foreground color of terminal different from the actual prompt, so using reverse-search (Ctrl+R) resets color of the line to the foreground, instead of color of the prompt specified in PS1.


  • 1
    Search this site, I know multiline prompts have come up before. You may have more luck with zsh instead of bash. – Gilles 'SO- stop being evil' Aug 16 '17 at 22:19

You'll need to repeat the escape coding at the beginning of the line that is losing its formatting:

PS1="\[\033[38;5;132m\]\n┌─■ testing\n\[\033[38;5;132m\]└─■ \[$(tput sgr0)\]"

Since other lines could be similarly affected it's best to apply escape codes after each newline. However, you don't need to apply them to an empty line. So here's what we end up with:

PS1="\n\[\033[38;5;132m\]┌─■ testing\n\[\033[38;5;132m\]└─■ \[$(tput sgr0)\]"

I tested and everything works for me. (And I have been using a colored multi-line prompt like this for a while w/o issues.)

Presumably what's going on here is that readline is overwriting that line (thereby resetting colors) and then redraws it with just that part of your PS1 value. Since you haven't explicitly applied color to that line you will get default color (e.g. white).

| improve this answer | |
  • It should be PS1="\[\033[38;5;132m\]┌─■ testing\n\[\033[38;5;132m\]└─■ \[$(tput sgr0)\]". The error (newline in the wrong place) was in the question and you have copied it. Look at the demo in the question. – MiniMax Aug 17 '17 at 8:22
  • @MiniMax Hmm. Fortunately the problem and solution are the same either way. I'll wait until OP fixes the question before updating my answer to match. Otherwise it could be confusing. Thanks. – B Layer Aug 17 '17 at 8:48
  • @MiniMax you're right, I missed the \n. @B Layer your second solution works great. Thanks. If you use reverse search does your prompt gets hidden on both lines or just on the last line, like mine? Since you use multi-line prompt I was wondering if there's a better way to define it. – seeker_of_bacon Aug 17 '17 at 12:48
  • @seeker_of_bacon Just the last line, like yours. After applying the fix, I think what you have is fine. (It's an interesting design. I sorta like it. :) – B Layer Aug 17 '17 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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