What I want is that the command can automatically add a new line when one line is full.
I tried \n but that just add a new line before command which is not the effect I want.
Now I tried PS1='[\e[0;34m[\u@\h \W]$ \e[m]', the effect is like:
CodePudding user response:
bash
can't tell how much space your prompt actually occupies on screen, because the ANSI escape sequences that set colors don't take any space. You need to enclose them (and only them) inside \[...\]
to tell bash
as much.
PS1='\[\e[0;34m\][\u@\h \W]$ \[\e[m\]'
bash
already knows how to handle its own escape sequences \u
, \h
, and \W
. The ANSI escape sequences only have special meaning to the terminal.
That is, \u
et al are expanded before bash
tries to determine how many characters are in the prompt. For all it knows, \e
, [
, 0
, ;
, 3
, 4
, and m
will all be displayed literally as single characters. The terminal sees them, and instead of displaying them, changes the color used to print the following characters.