Search code examples
linuxbashterminal

Bash prompt does not wrap text properly


I recently added some color to my bash prompt, and now when I type a command the text doesn't wrap properly.

Here is an example:

MacBook-Pro:~ williamgiles$
long commandwilliamgiles$ this is a really long command,this is a really 

My original PS1:

\h:\W \u\$

My new one with color:

\e[0;35m\h:\W \u$ \e[m

Solution

  • You need to mark the color-changing sequences as something that does not contribute to the length of the prompt on-screen.

    PS1='\[\e[0;35m\]\h:\W \u$ \[\e[m\]'
    

    bash already knows to take the expansion of \h, \W, and \u into account; it has no idea that \e[0;35m is not simply 7 characters that the terminal will display on screen.