I have a Bash prompt that takes a while (~100-200ms) to generate, because it includes Git working tree status that takes some time to compute. Sometimes I start typing a command before it appears, but then the command wraps incorrectly.

Here is what I see in the terminal:

# I start typing:
some command...

# then the prompt appears:
some command...username [branch*] some/directory $ some command...

# then I type some more, causing the command to wrap:
some command...username [branch*] some/directory $ some command...wra
pping text 1

# and when I continue typing, the text overwrites the second line:
some command...username [branch*] some/directory $ some command...wra
wrapping text 2

Any idea how to prevent this?

Here is how my Bash prompt is set up:

# in .bashrc:
PROMPT_COMMAND="PS1=\`/home/username/my-prompt.sh\`"

# in /home/username/my-prompt.sh:
$PROMPT=...
$PROMPT+=...
echo -ne $PROMPT

I believe I set up all my \[ and \]s correctly inside $PROMPT. The issue appears only when I start typing before the prompt appears, and my command wraps.

  • 1
    You could use a \r at the beginning of the prompt (inside the \[ \] part) to make the prompt always start at the beginning of the line. – wurtel Dec 4 '14 at 12:29
up vote 4 down vote accepted

It may work to disable terminal output while the prompt is generated:

PROMPT_COMMAND="PS1=\$(stty -echo)\`/home/username/my-prompt.sh\`\$(stty echo)"
  • That solves my problem, thanks. – hmp Dec 4 '14 at 16:09

I would suggest you to bring your terminal to sane mode(kidding) using

stty sane  # I hope it'll help and bring back the working of default prompt.
  • 1
    this may not be a good idea - that is not necessarily the terminal's default, and much of the effects dont apply to readline anyway because it will just reset it to raw mode and handle the rest on its own. stty sane should probably be considered a last ditch - not a default. – mikeserv Dec 4 '14 at 14:36
  • 1
    @mikeserv-Might be! – Am_I_Helpful Dec 4 '14 at 14:50
  • very good point. – mikeserv Dec 4 '14 at 14:51

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.