Tyilo
Tyilo

Reputation: 30102

Terminal not working with color-prompt

If I do this:

echo foo
PS1='\e[0;30mtest \e[0m'

and then presses the up arrow test PS1='\e[0;30mtest \e[0m' appears (as expected).
But if I press up arrow, so I should have echo foo, it results in test PS1='\e[0;3echo foo

If I set the PS1 to anything which doesn't include color it works:

echo foo
PS1='\e[0;30mtest \e[0m'
PS1='test '

Note: echo foo is still the command executed if I press Enter

I have tried this in both iTerm2 and the apple Terminal.

Why is it doing that and how do I fix it?

Upvotes: 2

Views: 673

Answers (2)

jw013
jw013

Reputation: 1808

PS1='\[\e[0;30m\]test \[\e[0m\]'
#    ^^        ^^     ^^     ^^

The \[ and \] tell bash about non-printing characters; otherwise it has no idea how long your prompt actually is, hence the mangled prompt. See the PROMPTING section of the bash manpage for more details.

Upvotes: 1

Chris Gregg
Chris Gregg

Reputation: 2382

Try this:

PS1="\[\e[0;30m\]test \[\e[0m\]"

By using extra \[ and \] brackets, you are telling bash that you've got some non-printing characters, which it may be misinterpreting when you press the up-arrow.

Upvotes: 4

Related Questions