Bash color prompt and long commands

2

1

I'm colorizing parts of my bash prompt using ANSI escape sequences. This works great, until the command I'm currently typing in is long enough that it has to wrap. Instead of the rest of the command displaying on the next line, it wraps back to column 1 of the current line, overwriting the beginning of the prompt.

I get that behavior with this prompt:

export PS1="[\u][\033[0;32;40mdemo \033[0;33;40m1.5.40.b\033[0;37;40m] \w> \033[0m"

but it works correctly with the same prompt, ANSI sequences remove:

export PS1="[\u][demo 1.5.40.b] \w> "

I'm connecting using the current version of Putty, with default Putty settings. The OS is Ubuntu 8.10.

Eric J.

Posted 2010-04-12T18:58:27.310

Reputation: 1 449

Answers

1

It must be something in there because the following string from the bash documentation does not exhibit the same behavior:

PS1="\[\033[1;34m\][\$(date +%H%M)][\u@\h:\w]$\[\033[0m\] "

Haven't figured out what it is yet, but their works and yours doesn't...

EDIT: Try:

PS1="[\u][\[\033[1;32m\]\[\033[1;40m\]demo \[\033[1;33m\]\[\033[1;40m\]1.5.40.b\[\033[0;37m\]] \w> \[\033[0m\] "

Josh

Posted 2010-04-12T18:58:27.310

Reputation: 7 540

3

That did it. The reason, it turns out, is that bash needs to be told "the following characters are non-printable" so it doesn't become confused over the length of the prompt. This is done by enclosing non-printable characters in \[ and \]. See http://tldp.org/HOWTO/Bash-Prompt-HOWTO/nonprintingchars.html

– Eric J. – 2010-04-12T20:13:03.713