jktstance
jktstance

Reputation: 165

Text input on command line does not match PS1

I set a custom PS1 to expand the current directory. That works well, however, the text input does not match the PS1 length. Instead of the input being at the ">" I set at the end of PS1, it is instead only about 10 characters in from the line start. This only happens when I'm inputting a fair amount of text on the command line (especially recalling previous long commands or pasting). If the text is short, everything works fine.

A lot of other weird stuff happens too, most often when the command being input requires a newline.

Is there something I missed when configuring .bashrc ?

Here is my PS1. 'CurDir' is a variable I set to print out the current directory (PWD if small, or a cut version if long). Most of the colors were from someone else. Only 'CurDir' was added by me and is set earlier in .bashrc.

PS1='\e[1m\[\u@\h \]\e[0;36m\[$CurDir\]\e[m>'

Upvotes: 1

Views: 237

Answers (1)

Benjamin W.
Benjamin W.

Reputation: 52506

You escaped the wrong parts: \[...\] is used to wrap the unprinted parts of the prompt.

Your escaping:

PS1='\e[1m\[\u@\h \]\e[0;36m\[$CurDir\]\e[m>'
            └─┬──┘            └──┬──┘
            escaped            escaped

Escaping the unprinted parts:

PS1='\[\e[1m\]\u@\h \[\e[0;36m\]$CurDir\[\e[m\]>'
       └─┬──┘         └───┬──┘           └─┬┘
       escaped        escaped             escaped

This assumes that there are no terminal escape codes in $CurDir.

Upvotes: 2

Related Questions