Bash Prompt – How to Customize PS1 Properly?

bashprompt

I customized my bash with this in my bashrc

export PS1="\e[0;36m\h\e[m \e[0;33m\w/\e[m \e[0;31m\n\$ →\e[m  "

So I get something like this (with colors) :

Ahuri ~/Public/ 
$ →  

But I am having problems with long commands. When I write a very long command that is longer than a line it starts overwriting my first line

Example :

Ahuri ~/Public/ 
$ → ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If I continue to add "^" I get:

Ahuri ~/Public/ 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

my "$ →" is overwritten, and then the whole line gets overwritten.

Best Answer

There is no issue with the \n. This is yet again the old escape sequence length problem: \e[0m and similar do not contribute to the actual length of the prompt, so you have to enclose them in \[..\] to indicate this to the interpreter:

PS1="\[\e[0;36m\]\h\[\e[m\] \[\e[0;33m\]\w/\[\e[m\]\n \[\e[0;31m\]\$ →\[\e[m\]  "
Related Question