tags:

views:

1714

answers:

6

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.

A: 

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
A: 

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
I tried this, but when i use the prevous commad keys, i get some residuemillermj@Leonidas~$find ./ -na
Milhous
A: 

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
+3  A: 

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
seems that is needed on the last line. not really required on a previous line.
Milhous
A: 

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
+1  A: 

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