0

This question already has an answer here:

I have set up a custom bash prompt so I can see stuff like the current git branch, npm package version info, the virtual env I'm in, etc. However, when i press the up arrow key to browse the history, the prompt glitches:

Here's what it is normally:

enter image description here

However, when I browse the history, the space between the lambda sign and the command disappears:

enter image description here

It only happens sometimes, and I don't know why. I guess I'm just being picky about a small space, but after spending a lot of time configuring my bash prompt I want it to look perfect.

This is what my .bashrc looks like. I'm using Git Bash for Windows, btw, if that makes a difference:

bashPrompt() {
SYMBOL="λ"

COUNT=(`find ./ -maxdepth 1 -name "package.json"`)

if [ ${#COUNT[@]} -gt 0 ]; then
    NPM_PACKAGE_INFO="($(node -p -e "require('./package.json').version"))"
else
    NPM_PACKAGE_INFO=""
fi

if [ ! -z "$CONDA_DEFAULT_ENV" ]; then
    ENV=" ($CONDA_DEFAULT_ENV)"
else
    ENV=""
fi

if [ -d .git ]; then
    if [ -z "$(git status --porcelain)" ]; then 
        echo "[\[\e[0;36;40m\u@\H\]\[\e[0;37;40m]\]\[\e[1;34;40m${ENV}\] \[\e[1;31;40m\w\]\[\e[1;32;40m$(__git_ps1)\] \[\e[0;37;40m${NPM_PACKAGE_INFO}\]\n\[\e[0;37;40m${SYMBOL}\] "
    else 
        echo "[\[\e[0;36;40m\u@\H\]\[\e[0;37;40m]\]\[\e[1;34;40m${ENV}\] \[\e[1;31;40m\w\]\[\e[1;33;40m$(__git_ps1)\] \[\e[0;37;40m${NPM_PACKAGE_INFO}\]\n\[\e[0;37;40m${SYMBOL}\] "
    fi
else
    echo "[\[\e[0;36;40m\u@\H\]\[\e[0;37;40m]\]\[\e[1;34;40m${ENV}\] \[\e[1;31;40m\w\] \[\e[0;37;40m${NPM_PACKAGE_INFO}\]\n\[\e[0;37;40m${SYMBOL}\] "
fi;

}

PROMPT_COMMAND='PS1="$(bashPrompt)"'

marked as duplicate by Jeff Schaller, RalfFriedl, G-Man, filbranden, JigglyNaga Nov 25 '18 at 8:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \[ and \] should only enclose things that occupy zero space on the screen, such as escape sequences that change the text color. Please try moving everything that takes up space, such as \u@\H and ${SYMBOL}, out of those braces. – Mark Plotnick Nov 23 '18 at 21:06
  • What if it varies in length. It could be zero length or it could be more? What then? Do I keep the \[ and \] – Yash Patel Nov 23 '18 at 22:29
  • 1
    Zero length as in "the string may have zero characters in it" is fine and should be outside of \[ and \]. I should have said "\[ and \] should be used to enclose a string of n (n > 0) characters that will take up zero space on the screen. If you do not use \[ and \], bash will assume they take up n character positions on the screen, and redisplay may be messed up." – Mark Plotnick Nov 23 '18 at 23:09
  • @Mark that is an answer. You could write it up better though. If it was an answer, then I would have done a few edits, for you. – ctrl-alt-delor Nov 24 '18 at 0:11
  • 1
    This is unix.stackexchange.com/questions/317734 again. – JdeBP Nov 24 '18 at 0:59
0

One of your string reads:

"[\[\e[0;36;40m\u@\H\]\[\e[0;37;40m]\]\[\e[1;34;40m${ENV}\] \[\e[1;31;40m\w\]\[\e[1;32;40m$(__git_ps1)\] \[\e[0;37;40m${NPM_PACKAGE_INFO}\]\n\[\e[0;37;40m${SYMBOL}\] "

In which you are enclosing every text inside \[\] pairs. If you remove the newline \n and try that PS1:

PS1="[\[\e[0;36;40m\u@\H\]\[\e[0;37;40m]\]\[\e[1;34;40m${ENV}\] \[\e[1;31;40m\w\]\[\e[1;32;40m$(__git_ps1)\] \[\e[0;37;40m${NPM_PACKAGE_INFO}\] n\[\e[0;37;40m${SYMBOL}\] "

You will find that most of the prompt will be replaced by the history commands if you press up (up arrow).

Just place text that is meant to use line space out of those brackets:

PS1="[\[\e[0;36;40m\]\u@\H \[\e[0;37;40m]\]\[\e[1;34;40m\]${ENV} \[\e[1;31;40m\]\w\[\e[1;32;40m\]$(__git_ps1) \[\e[0;37;40m\]${NPM_PACKAGE_INFO} n\[\e[0;37;40m\]${SYMBOL} "

And try again.

After that works correctly, add the newline \n back and try again.

If still in trouble, update bash to 4.4.18 (present built) or similar.

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