Dinesh
Dinesh

Reputation: 4559

2-line bash prompt - why can the user backspace entire second line including prompt characters

I have a bash prompt as follows:

$ echo $PS1
\[\033[1;31m\]\t \[\033[1;32m\]\u@\[\033[0;32m\]\[\033[4;32m\]\h\[\033[m \[\033[1;36m\]\w \[\033[0;33m\][\!] \[\033[0;36m\]{e=$?}\n\[\033[m\$ 
01:51:41 dinesh@c1 ~/lab [1030] {e=0}
$

But if I were to type something and then backspace, I can backspace all the way to the start of line, erasing even the "$ ". Why is that, how can it be made to stop backspacing at "$ "?

However if I changed it to:

PS1="LINE1\nLINE 2> " 

then the backspacing stops at where prompt ends.

GNU bash, version 3.2.51(1)-release (x86_64-suse-linux-gnu)

Upvotes: 1

Views: 273

Answers (3)

Robin Hsu
Robin Hsu

Reputation: 4484

Move around your last \n to be right before \$ will fix the problem:

Your original one (leading part omitted)

....\n\[\033[m\$

Changed to this (leading part omitted):

....\[\033[m\n\$

Upvotes: 0

ooga
ooga

Reputation: 15501

You're missing the final \] before the last m. You need \[ and \] around all non-printing characters, but not around the printing ones. Since you're missing the closing \], the dollar sign is not calculated as a printing character.

Upvotes: 1

that other guy
that other guy

Reputation: 123470

You have to enclose all the non-printing ANSI escape sequences in \[...\]. You're missing a \] for the last one.

Upvotes: 4

Related Questions