1

When I set PS1 variable in ~/.bash_profile, it worked as configured. However, the history is somehow messed up.

When I press Arrow Up or Ctrl+P to view commands I typed, it starts to duplicate partially at some points. The scenario is displayed below(pay attention to cd /Co which is the duplicated part):

enter image description here

Although visually duplicated, when press enter I still get the correct result in history commands.

Here's my system information:

  • OS: Mac OS X 10.9.4
  • Terminal: system default terminal, iTerm 2

I set PS1 in my .bash_profile like this:

PS1='${YELLOW}\u${WHITE} in ${GREEN}\w${WHITE}${YELLOW}`__git_ps1 " on 
%s"`${WHITE}\r\n${NORMAL}$ '
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
jasonslyvia
  • 111
  • 3

1 Answers1

1

You need to put \[ and \] around all escape sequences so that the line editor knows how long the prompt is.

o11c
  • 1,408
  • 7
  • 13
  • i'm sorry i know it's ashamed to ask but i don't really understand bash stuff, i change the PS1 into – jasonslyvia Oct 13 '14 at 07:03
  • `PS1='${YELLOW}\[\u\]${WHITE} in ${GREEN}\[\w\]${WHITE}${YELLOW}`__git_ps1 " on %s"`${WHITE}\[\r\]\[\n\]${NORMAL}$ '` but it seems not wokring – jasonslyvia Oct 13 '14 at 07:05
  • @jasonslyvia no, around the ${YELLOW} and such. Back already knows how to handle \u and such. – o11c Oct 13 '14 at 21:53
  • that's very helpful, thank you so much! For future reference, the working example would be: `PS1='\[${YELLOW}\]\u\[${WHITE}\] in \[${GREEN}\]\w\[${WHITE}\]\[${YELLOW}\]`__git_ps1 " on %s"`\[${WHITE}\]\r\n\[${NORMAL}$\] '` – jasonslyvia Oct 14 '14 at 05:52