6

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.

brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • 1
    Your syntax is atrocious. At cursory glance, you don't have a hard bracket to close it all off (before the $). There are other problems. I suggest starting over. Here's a guide to colorization: http://www.ibm.com/developerworks/linux/library/l-tip-prompt/ –  Sep 25 '11 at 03:25
  • 2
    Side note: When customizing the prompt, use `\$` rather than a literal dollar sign. Bash will display the appropriate prompt character (`$` for normal users, `#` for root). – Chris Page Sep 25 '11 at 08:25
  • Another style tip: don't hard-code escape sequences (I realize most people do, but they shouldn't). Instead, use `tput` to emit the appropriate codes for the current terminal, so that it works appropriately with various terminals. In particular, it will output nothing if the terminal doesn't support the colors/styles in your prompt, rather than emit characters that may confuse the terminal. e.g., `PS1="\[$(tput setaf 1)\]\h:\W\[$(tput sgr0)\] \u\$ "` will display the hostname and working directory in red. – Chris Page Sep 25 '11 at 08:37
  • possible duplicate of [Mac Terminal.app annoying bug - How to fix it?](http://stackoverflow.com/questions/1511694/mac-terminal-app-annoying-bug-how-to-fix-it) – Chris Johnsen Sep 26 '11 at 01:26
  • In case you want to check the link from the first comment, here is an archived version: http://web.archive.org/web/20111018102445/http://www.ibm.com/developerworks/linux/library/l-tip-prompt/ – brandizzi Jul 14 '22 at 15:01

1 Answers1

5

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.)

Community
  • 1
  • 1
Chris Page
  • 18,263
  • 4
  • 39
  • 47
  • 3
    Good answer but I think it's acceptable to leave it on Ask Different as Apple users with this issue are less likely to go to Stackoverflow for an answer – conorgriffin Sep 25 '11 at 08:30