Pages

7/20/2012

Fixing Blackbuntu Command Line Wrapping Problem

I have been experiencing this problem for quite sometime now. It is when I type a long command (> 20 chars ), the whole line then disappears.  I have to re-type the whole line again, or pressing home then space to make it appears again.


I searched and found this article http://www.ibm.com/developerworks/linux/library/l-tip-prompt/ . And I came to the conclusion that this is a problem about coloring in bash and line wrapping.

Root cause is, coloring codes are not enclosed in bracket \[...\] which means Bash(or Xterm, I'm not sure) have to count the coloring code characters as characters on the line and does line-wrapping using the count. And this messes up the whole line.

Solution:
  1. find every file that setting the color for bash prompt. Possible files are:
    1. ~/.bashrc                <<< I found my prompt setting in here
    2. /etc/profile
    3. /etc/profile.d/*.sh
    4. /etc/bash.bashrc
  2. To check if your prompt setting in either of the files, look for the word "PS1"
  3. If there is any color code such as  
    ### Prompt 
    ((UID)) && User="\e[1;32m" || User="\e[1;31m"
    PS1="$User\u\e[0;34m@\h\e[0;37m\w$User\\$\e[m "
    

    Enclose every color codes with \[...\], so they become
    ### Prompt 
    ((UID)) && User="\[\e[1;32m\]" || User="\[\e[1;31m\]"
    PS1="$User\u\[\e[0;34m\]@\h\[\e[0;37m\]\w$User\\$\[\e[m\] "
    
  4. Save the file and you're done.
Next time you open your bash prompt again, it'll be problem-free  :)



1 comment:

  1. Wow...thanks! This has been driving me crazy for over a year.

    ReplyDelete