7

I am using bash in os X Terminal app, and my custom $PS1 breaks when I scroll through my history.

PS1="${BLUE}\u${CYAN}@${RED}\h${BLUE}\w\n\[${red}\$${NC}\]"

also tried PS1="${BLUE}\u${CYAN}@${RED}\h${BLUE}\w\r\n[${red}\$${NC}]"

The problem seems to be in the newline. I have used this bash prompt on Slackware no prob.

Milhous
  • 14,473
  • 16
  • 63
  • 82

6 Answers6

9

You need the [ and ] arond every escape sequence; do $BLUE and the like include these? If not, they need to be bracketed with these calls.

Ben Stiglitz
  • 3,994
  • 27
  • 24
  • seems that is needed on the last line. not really required on a previous line. – Milhous Sep 20 '08 at 04:16
  • 1
    for others like me to whom "you need the [ and ] around every escape sequence" doesn't mean much, what you need is: if you had (like me): `PS1="\e[0;32m\w \$\e[m "` you need: `PS1="\[\e[0;32m\]\w \$\[\e[m\] "` – ekkis Apr 10 '19 at 23:20
  • Note, in modern MacOS, when using `zsh`, the escape sequence is not `\[...\]`, but `%{...%}` – astax Jan 03 '23 at 16:16
2

To avoid such 'escaping' difficulties as you prompt needs evole to be more complex, this should be a skeleton to start growing on:

function _my_prompt ()
{ 
  # magic goes here
  my_prmpt=.... 
}
PROMPT_COMMAND='_my_prompt'
PS1="[\$my_prmpt] \$"
Hedgehog
  • 5,487
  • 4
  • 36
  • 43
2

I was having the same problem when logging on remote (debian) systems. As the escaped values in .bashrc all were nicely bracketed, I did some googling and discovered that the cause might be differences in window size on the local and the remote system. Adding

shopt -s checkwinsize

to .bashrc on the remote systems has fixed the problem for me.

Source: http://forums.macosxhints.com/showthread.php?t=17068

user247669
  • 21
  • 1
0

If the problem seems to be with the newline, try putting \r\n instead of just \n and see if it makes a difference.

apandit
  • 3,304
  • 1
  • 26
  • 32
0

I get the same problem (on OS X) with your PS1. If I remove the \[ and \]

PS1="${BLUE}\u${CYAN}@${RED}\h${BLUE}\w\n${red}\$${NC}"

this works fine. Are the sqare brackets needed? I've never used them, but from the docs:

\[ Begin a sequence of non-printing characters. This could be used to embed a terminal control sequence into the prompt.

\] End a sequence of non-printing characters.

dF.
  • 74,139
  • 30
  • 130
  • 136
  • I tried this, but when i use the prevous commad keys, i get some residue millermj@Leonidas~ $find ./ -na – Milhous Sep 19 '08 at 21:28
0

I have now tried

PS1="${BLUE}\u${CYAN}@${RED}\h${BLUE}\w${RED}\r\n\$\[${blue}\]"

Which seems to work The brackets needed to make previous commands work.

Milhous
  • 14,473
  • 16
  • 63
  • 82