Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My bash prompt, which I'll admit to have stolen from a few places and cobbled together, will sometimes add part of previous commands to its length when scrolling the bash history with up/down arrows.

For example, if my previous commands were:

cd /home/caleb
vim .bashrc

When I was at my prompt and scrolled up twice it might look like:

$ vim .bcd /home/caleb

Where the first five characters are left over from last command.

Does anyone have any idea why this is happening, and how it can be stopped?

My prompt is set with this code (way to long to include here):

share|improve this question
Set PS1 to a value without the whole vcs crap and see what happens. That's my guess. – Daniel Beck Jan 26 '12 at 0:44
Have you already found the culprit in your prompt? I'm having the same issue. – acme Jul 10 '14 at 8:58
Yeah bash loses it on colors, and is unable to separate the length of strings with color escapes from the length of the visible string. This is what SiegeX was getting at. I ended up switching to ZSH and using a different prompt. ZSH doesn't have the same issue. – Caleb Thompson Jul 11 '14 at 13:45
Both previous answer didn't get my problem solved, and did not give any explaination why this happened. Please check Custom Bash prompt is overwriting itself, if anyone have search to this point. – jujj Dec 22 '14 at 3:05
up vote 4 down vote accepted

Somewhere your prompt is fubar. What usually happens is that your shell thinks its outputting non-printable term codes and expecting it to take up space. The best advice I can give you is to systematically add to (or take away from) your prompt until this behavior stops to isolate the code that is causing this issue.

share|improve this answer

I had the same problem and it was related to the color definitions.

In my case, I have a multi-line prompt (gives most space for current command regardless of path length displayed by prompt).

Bad version:

export PS1="\n\n\[\033[01;33m[\w]\n\033[00m\$ "

Good version:

export PS1="\n\n\[\033[01;33m[\w]\033[00m\n\$ "

\033[00m terminates the color. If it is after the new line (\n), it prevents proper redraw in the terminal, to overwrite previous commands with background color. Moving it behind the new line resolved the problem.

(using Terminal in Mac OS 10.8)

share|improve this answer
This pinpointed the issue for me, while the current accepted answer was too generic. – Brian Dec 20 '14 at 17:38
This is the more precise answer and should be the winner (and was the solution to my problem as well). – craveytrain Apr 24 '15 at 15:49
the \n were the culprit for me too. Thanks! – mhulse Mar 12 at 9:05

The color codes need to be wrapped in square brackets. The brackets inform bash that the enclosed text should not be printed

building on @Phreditor's example, this shows that any formatting done after the newline will result in the original issue:

export PS1="\n\n\[\033[01;33m[\w]\033[00m\n\033[0;90m\$ "

wrapping the format code in [] ensures that annoying behavior never happens:

export PS1="\n\[\[\033[01;33m\][\w]\[\033[00m\]\n\[\033[0;90m\]\$ "

The documentation:

Since PS1 formatting causes the value to be so long and hard to read I put the format codes in variables:

export PS1="\n${BYELLOW}[\w]${PS_CLEAR}\n${IBLACK}\$ "
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .