Linux: coloring bash prompt will break carriage return

10,873

Solution 1

You must wrap all escape sequences between \[ and \] in order for 'readline' to correctly calculate the prompt's visible length. Without them, 'readline' thinks the prompt is longer than it is, and doesn't know when the text reaches the margin.

PS1="\[\e[;31m\][\u@\h \W]\$ \[\e[m\]"

Solution 2

the [ and ] tip worked on bash on OS X 10.8.2 too.

I use:

PS1="[\e[0;37m\W\$(git branch 2>/dev/null | grep -e '* ' | sed 's/^..(.*)/{\1}/') \$ \e[m ]"

which shows me my current git branch in use.

Share:
10,873

Related videos on Youtube

Abonec
Author by

Abonec

Updated on September 18, 2022

Comments

  • Abonec
    Abonec almost 2 years

    When I try to colorize the bash prompt like this:

    export PS1="\e[0;31m[\u@\h \W]\$ \e[m "
    

    it breaks the carriage return (instead of going to a new line when line is full, it starts in the same line and erases the input text).

    If I remove the color sequence it works fine.
    I use Ubuntu 11.10 and terminal (guake has the same bug too).

    • Matteo
      Matteo over 12 years
      Strange. It works w/o any problem on RedHat 5 and Mac OS X
    • user1686
      user1686 over 12 years
      possible duplicate of PS1 problem, cmd is looping
  • osirisgothra
    osirisgothra over 11 years
    Thanks a ton!! i was trying to find some documentation on it in the official docs but couldnt -- the 'stock' color prompt does employ this 'trick' and I had a hard time at first figuring out what exactly it was for, so thanks for clearing that up --- btw i did actually find the documentation source in 'man readline 3' around line 684 and it says that this is actually called 'skip-csi-sequence' for those who want to know where the documentation on this is. I don't think i would have found it without you pointing me to readline(3) even though i knew about it before-failed to make the connection.
  • rafak
    rafak about 11 years
    Do you know about "__git_ps1" ?
  • stonefruit
    stonefruit about 11 years
    nopes. what magical thing will it do?
  • Max Howell
    Max Howell over 10 years
    What does "wrap" mean? Do you mean wrap the whole thing IN a \[, \]? I guess so, but it doesn't seem to be working for me.
  • user1686
    user1686 almost 10 years
    @osirisgothra: The documentation is actually talking about a different thing – skip-csi-sequence is meant to suppress input sequences generated by special keys.
  • Enrico
    Enrico about 8 years
    @MaxHowell you have to wrap each \e[xxm sequence, not the whole string.
  • IC_
    IC_ about 6 years
    Thanks. In my case I just replaced \e[39m with \[\e[;39m\] and it works fine
  • user1686
    user1686 about 6 years
    Note that \e[;39m expands to \e[0;39m which means "reset all formatting, then set color to 39". It's good in prompts, but it is not in any way related to my post about \[ \].
  • Rudolf Adamkovic
    Rudolf Adamkovic about 3 years
    Thank you so much. I was going crazy debugging this...