Bash prompt – long command circle back to same line after adding color

bashcolorsprompt

Note: I am using Putty and my TERM is set to XTERM.

I have added the color to my bash prompt as

PS1="\[\033[0;32m\]\d \t \u\e[1;33m@\H /\W $ \[\033[1;37m\]"

just to simplify PS1 in English –

${GREEN}\d \t \u{YELLOW}@\H \W $ {reset-color}

Now, whenever my command is long and doesn't fit into the line of screen, it should come over to the next line, but it doesn't and the initial character start getting replaced which each character I type. The whole command is present but is not seen completely. It executes normally, so it's just the display that is getting distorted.

The situation worsens when I go back to the first character and try to delete the characters, the whole command line moves up and up erasing the previous data that was on screen.

After some trial and error removing some-part I found that addition a @ leads to the above behavior

\e[1;33m

I really don't remember why I had put this and I am not able to trace this back.

Can you help me in resolving this issue? The long commands should get on the next line and not on the same line.

Best Answer

The ANSI escape sequence right before the “@” sign is not enclosed in \[ and \]. Enclose it and should be fine:

PS1='\[\033[0;32m\]\d \t \u\[\e[1;33m\]@\H /\W $ \[\033[1;37m\]'

By the way, what you call “reset-color” is just setting it to white. To actually reset it use \033[0m.

Related Question