Search code examples
bashcolorsterminalcygwinprompt

cygwin terminal prompt color causes havoc


I am sorry because this is not really a programmer question (well it becomes one when my bash script needs to change the prompt color), but this inconvenience has been bugging me ever since I started testing my code on Cygwin. When I type a very long command line with a lot of arguments, it breaks to the next line after 61 characters, not at the end of the line (80 in my case), as you would expect it to. When you use backspace, home, end, or left/right arrow keys, the cursor doesn't move as it should. Also, if one of your previous commands in your command history is long (longer that 61 characters), pressing up arrow key after reaching that command will produce a scrambled line. I first though this was a bug in Cygwin and tried to ignore it. After some (very long) research, I found out that the problem was in my profile file, and further research showed that the problem is caused by my prompt coloring.

Apparently, this is the cause of all the trouble:

PS1="\e[0;32m\w> \e[1;32m"

But this works quite fine (except it's the wrong color):

PS1="\w> "

Any ideas what might be the problem? Am I doing something wrong here?


Solution

  • I figured this out a while ago so I can't exactly explain it, but it needs to be like this:

    PS1='\[\e[0;32m\]\w> \[\e[1;32m\]'
    

    Basically you need to surround the escape sequences with \[ and \].