Question

I started to manage a new server based on CentOS. I wanted to change the prompt, so I wrote the following command :

PS1="\e[0;36m[`pwd`]\$\e[m "

It worked perfectly. But since I got an annoying issue. When I write something quite long, or displays an older command that is quite long using up arrow, or paste it, and then click on "home" to get to the top of the line, the cursor stops within the command, 10 characters ahead of the prompt. For example, lets say I write this :

[/]$ git log --pretty=oneline

And then click on home button, the cursor will stop on the "p" letter, after "--". And if I try to move with left key to get on top of the line, it does that annoying bip saying "you are already there, mate"...

Now, lets say I wrote

ls

and right after

git log --pretty=oneline

If I go up two times, the prompt displays this :

[/]$ git log --ls

And if I keep going up the "git log --" never goes away. Now if I press enter, it will still launch the ls command without any problem. Seems like it's just a display issue. But still, that confuse me all the time...

Thanks ahead for your help !

Was it helpful?

Solution

Use \w to print the working directory in your shell prompt, rather than trying to embed a command.

You also need to escape the escape sequences so that bash doesn't attempt to count them as printed characters. This is done by enclosing them with \[ and \].

So you should end up with something like:

PS1="\[\e[0;36m\][\w]\$\[\e[m\]"

Your prompt is also very compact, you may want to stick some spaces in it. The key is that you have used the brackets to escape the non-printable characters.

You can find a complete list of these substitutions in the PROMPTING section of the bash man page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top