Custom prompt doesn't work on Mac Terminal

4

2

I like to use a custom prompt (current path in blue) on my unix machine:

export PS1='\[\e[0;34m\]\w \$\[\e[m\] '

But when I try to use it on Mac's terminal it doesn't work: it fails to detect the end of the prompt and overwrites the prompt when I type commands. This also happens when I'm inputting a long command where it wraps over the same line instead of starting a new line.

I don't understand why this is the case since I use bash on both machines. Any suggestions on how to remedy this?

mareks

Posted 2012-09-08T15:05:52.627

Reputation:

Answers

3

I'm not sure why you are getting different results, but the correct value should be

export PS1='\[\e[0;34m\]\w \$\[\e[m\] '

Non-printing characters should be enclosed in \[ \], so that bash can compute the correct length of the prompt on the screen (for cursor-positioning purposes).

chepner

Posted 2012-09-08T15:05:52.627

Reputation: 5 645

Yes, export PS1='[\e[0;34m]\w $[\e[m] ' is the value I'm using. I somehow miscopied it in my question. It doesn't work as I explained above. – None – 2012-09-08T15:13:32.230

It works for me. – Paul R – 2012-09-08T15:16:32.690

@mareks In your comment above, you again seem to be missing the backslash before the [ and ] that enclose the ANSI escape codes for the colors. \[\e[, not [\e[. – chepner – 2012-09-08T16:37:09.683

2

Try putting it in regular double quotes "" instead of these ' ' single quotes. Worked for me. Also works with iTerm as well as Mac OS X's terminal.

Wolfgang Levinson

Posted 2012-09-08T15:05:52.627

Reputation: 21

0

\[\e[ became [\e[ in the post when it wasn't formatted as code. Sorry about the confusion. I solved the problem by updating bash from 3.0 to 4.2 on my mac.

mareks

Posted 2012-09-08T15:05:52.627

Reputation: 11