2

I tried to change my prompt to my own preferences, but when I'm switching through the commands I recently entered (with key-arrows), sometimes the whole line with the prompt is deleted only some chars of the prompt with the chars of the command I recently entered remain:

Expected Output:

bucky in [~]: cd ..

Output:

buccd ..

'buc' is part of the prompt, 'cd ..' is a command I recently typed.

My prompt:

PS1="\[\033[0;34m\u\] \[\033[0;37min\] \[\033[1;32m[\]\[\033[0;31m\w\]\[\033[1;32m]\]\[\033[0;37m:\] "

To give you a visualization here is an image:

enter image description here

If needed, I'm using Ubuntu 16.04

Uuser
  • 23

3 Answers3

4

This is because the printing parts of the prompt \u in \w and : are in escaped square brackets. The brackets are normally placed around non-printing characters to tell bash not to move the cursor for them and thus correctly calculate the size of the prompt. So now the cursor is not being moved correctly for the printing sections.

I recommend you modify the parts of your .bashrc that set the prompt rather than overriding it... you could uncomment (remove the #) this line:

#force_color_prompt=yes

and then modify the second line in this snippet:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

so it says:

PS1='${debian_chroot:+($debian_chroot)}\[\033[00;34m\]\u \[\033[0;37m\]in \[\033[01;32m\][\[\033[0;31m\]\w\[\033[1;32m\]]\[\033[0;37m\]: '

Or if you prefer just put your override at the end of the file:

PS1='\[\033[00;34m\]\u \[\033[0;37m\]in \[\033[01;32m\][\[\033[0;31m\]\w\[\033[1;32m\]]\[\033[0;37m\]: '

This achieves what I think you want for me in Ubuntu 16.04 and doesn't break in the way you describe (which I have also experienced before when playing with my PS1!)

Zanna
  • 3,571
  • It helped a lot, thank you, the problem was the misplacement of the brackets, I corrected it and overwrote it. Now it works. It seems a little bit strange to me, that I could only notice the issue after the browsing of the history. – Uuser Aug 30 '16 at 21:41
0

Wrong use of squared brackets.

PS1="\[\033[0;34m\]\u \[\033[0;37m\]in \[\033[1;32m\][\[\033[0;31m\]\w\[\033[1;32m\]]\[\033[0;37m\]: "
Ipor Sircer
  • 14,546
  • 1
  • 27
  • 39
0

It was the brackets that were messing you up. In particular the one un-escaped "[" at the beginning of the working directory. "[\w" Try this:

PS1="\033[0;34m\u \033[1;32m\[[\033[0;31m\w\033[1;32m]\033[0;37m: "

It does exactly what you want in my terminal.

bashBedlam
  • 1,049