Skip to content
Advertisement

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>'

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement