While modifying the definition of my PS1, I saw that "\[" and "\]" markers should be added to help bash to compute the right display lenght. Many exemples on the web do not use them or even mention them.

I searched for a solution to add them automatically, like with sed, but I didn't find any example.

Are they still needed and is there a recommandation not to use sed to define PS1?

This is what I use at the moment:

PS1="$(echo -n "$PS_" | sed -e 's/\\e\[/\\\[\\e\[/g' -e 's/[0-9]m/&\\\]/g')"
link|improve this question
feedback

1 Answer

up vote 0 down vote accepted

Yes, \[ and \] still are needed, otherwise you'll run into ugly glitches when the command is wider than your terminal window.

link|improve this answer
That's what I have read for the escape chars, but what about the use of sed to define PS1? – PRouleau Aug 30 '11 at 13:42
The thing is, not all of them end with a 'm'... If you use the escapes purely for formatting, it's okay - it's your bashrc, do what you want. But it's not a very clean way. Many other users instead make variables like red='\[\e[31m\]' and then have PS1="${red}\u${norm} ..." Nice and clean. – grawity Aug 30 '11 at 13:50
Wheel, I also use variables to keep the definition easier to read, but all the .bash_color files I saw defined them without the "\[\]". So I am wondering. Thanks for your help. – PRouleau Sep 8 '11 at 0:27
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.