1

I'm using Gnome Terminal to connect via SSH to my server. My account on the remote server (which is Ubuntu) uses bash. My bashrc contains a line that sets the shell prompt to include my current git branch if applicable:

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h: \[\033[32m\]\w\[\033[00m\]\$(parse_git_branch)\$ "

When I am inside a git repo, my terminal stops wrapping properly. Long commands that should wrap onto the second line instead wrap to the beginning of the line they started on and start printing right on top of my prompt.

I don't experience wrapping issues except when my working directory is inside a git repo (i.e., when parse_git_branch's output is not empty).

I know that non-printable characters should be enclosed in \[ and \] so that the shell (or terminal?) knows not to count them towards the length of the prompt. I'm fairly confident that the "export" line has correctly done that... but my regex-fu is not strong enough to understand what that sed command is doing, and I think it isn't inserting the non-printable characters properly.

This question is extremely similar (and even uses the same parse_git_branch function, which we got from here), but the other question involved having an emoji in the prompt, which is not the case for me.

Does anyone know what is wrong with my prompt?

2 Answers2

3

This turned out to be because I had enabled the ui color=always option in git, not directly because of my shell prompt or the parse_git_branch function. The "git branch" command was displaying with color, but the original author of the parse_git_branch function did not account for that and chopped the string up in such a way that the color codes didn't get closed properly.

Adding the --no-color flag to the "git branch" command inside of the parse_git_branch function was enough to fix the issue, but I had actually grown accustomed to the colors -- so here is how to replicate the gold background:

parse_git_branch() {
     git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h: \[\033[32m\]\w\[\033[00m\]\[\033[7m\033[33m\]\$(parse_git_branch)\[\033[00m\]$     "
2

When you install git package. It should install a local file that you can source which has the branch parsing built in. Have you tried using that instead? I use this with no issues. This is taken from RHEL, so git-prompt.sh might be in a different location on ubuntu. Just do a "locate git-prompt.sh", alternatively looks like a version is available here: https://github.com/magicmonty/bash-git-prompt

. /usr/share/git-core/contrib/completion/git-prompt.sh
export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
abe
  • 31