Search code examples
macosterminalcommand-lineiterm

Setting colors in Terminal leads to strange character line limit


I found an annoying bug while coloring the prompt of my Terminal. If I set my prompt to a colored one, such as

export PS1='\e[1;34m[\e[0;31m\D{%Hh%M} \e[0;32m\u\e[0m@\e[0;35m\h\e[0m:\e[0;36m\w\e[1;34m]\e[0m $ '

then it starts to break when I get some size in the input line:

enter image description here

In other words, when my line reaches some limit, it starts over itself! Once I fill the same line again, then it works well, going to the next line.

Have anyone seen this problem, too? Do you have a solution? The problem also happens in iTerm.


Solution

  • This is a duplicate of Mac Terminal.app annoying bug - How to fix it? from StackOverflow. The problem is that you must surround terminal control characters in square brackets \[ … \] so that the bash shell doesn't count them when calculating the length of the command prompt.

    Since this is a generic shell/terminal question and not specific to Mac OS X or Terminal, this should probably be migrated to StackOverflow and made a duplicate of the other question. (However, I don't have privilege to do either.)