7

I customized my bash with this in my bashrc

export PS1="\e[0;36m\h\e[m \e[0;33m\w/\e[m \e[0;31m\n\$ →\e[m  "

So I get something like this (with colors) :

Ahuri ~/Public/ 
$ →  

But I am having problems with long commands. When I write a very long command that is longer than a line it starts overwriting my first line

Example :

Ahuri ~/Public/ 
$ → ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If I continue to add "^" I get:

Ahuri ~/Public/ 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

my "$ →" is overwritten, and then the whole line gets overwritten.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Ahuri3
  • 85
  • 1
  • 1
  • 6

3 Answers3

9

There is no issue with the \n. This is yet again the old escape sequence length problem: \e[0m and similar do not contribute to the actual length of the prompt, so you have to enclose them in \[..\] to indicate this to the interpreter:

PS1="\[\e[0;36m\]\h\[\e[m\] \[\e[0;33m\]\w/\[\e[m\]\n \[\e[0;31m\]\$ →\[\e[m\]  "
manatwork
  • 30,549
  • 7
  • 101
  • 91
1

A simpler option is to use the tput sequences:

export PS1='\[$(tput setaf 4)\]\h\[$(tput sgr0)\] \[$(tput setaf 3)\]\w/\[$(tput sgr0)\]\n\[$(tput setaf 1)\]\$ →\[$(tput sgr0)\] '

The \[ and \] enclose the terminal control sequences inserted by the command substitions ($(tput … )) so that the shell does not count them as printable output. Using the command substitutions rather than hardwiring stuff further ensures that you get the right control sequences for whatever your terminal type is.

JdeBP
  • 66,967
  • 12
  • 159
  • 343
l0b0
  • 50,672
  • 41
  • 197
  • 360
  • 1
    Are you sure? `tput` not seems to add anything special to solve the length calculation: http://pastebin.com/u28RTT5t – manatwork Apr 03 '13 at 12:37
  • l0b0 has left unstated that xe has also added the missing `\[` and `\]`. But this is the better answer (_with_ that omission stated) because the world largely doesn't use boldface to change colour any more. _Boldface means boldface_. Hardwiring control sequences in imitation of manatwork's answer often nowadays hardwires _the wrong control sequences for changing colour_; as people think of `0;` and `1;` as magic, rather than reset-all and boldface. – JdeBP Sep 11 '20 at 07:53
0

Use $PROMPT_COMMAND to display the additional line so you have no \n in $PS1.

choroba
  • 45,735
  • 7
  • 84
  • 110