2

I use the following entry in ~/.bashrc file to colorize the prompt and display current branch of git repository:

PS1='\[\e[1;32m\]\[\u@\h\]\[\e[m\] \[\w\]\[\e[1;1m\]\[$(__git_ps1 " (%s)")\] \[\e[1;1m\]\[$\] \[\e[m\]'

This works almost fine, except when I use bash history (up arrow key few times), the command line becomes 'outdented' (just the first characters of the prompt remains untouched), and the visible is:

usemmand

when my username is user and the command is command.

takeshin
  • 1,431
  • 3
  • 19
  • 28

3 Answers3

4

\[ and \] should only be used around parts of the command prompt that do not advance the cursor, despite having actual text. if __git_ps1 produces any visible output then this will desynchronize your command line.

Ignacio Vazquez-Abrams
  • 45,019
  • 5
  • 78
  • 84
2

This is the correct version:

COLOR1="\[\e[1;32m\]"
COLOR2='\[\e[1;1m\]'
COLOR3='\[\e[m\]'
GIT_STATUS=$(__git_ps1 " %s")
PROMPT_CHAR="$"

PROMPT="${COLOR1}\u@\h${COLOR3} \w${COLOR2}${GIT_STATUS} ${COLOR2}${PROMPT_CHAR} ${COLOR3}"
PS1="$PROMPT"
export PS1
takeshin
  • 1,431
  • 3
  • 19
  • 28
1

Anyone working with this issue, here is the example that worked for me:

BEFORE:

export PS1="\033[0;33m[\u@\h]\033[1m\e[m \w \033[36m[>>]\033[0m\e[m "

AFTER:

export PS1='\[\033[0;34m\][\u@\h]\[\033[1m\e[m\] \w \[\033[35m\][>>]\[\033[0m\e[m\] '

Notes all the '\\[\\]' around the colors.

Wesley
  • 32,320
  • 9
  • 80
  • 116