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.

Ahuri3
  • 85

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
  • 31,277
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
  • 68,745
l0b0
  • 51,350
  • 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
  • 47,233