Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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] \$"

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

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top