-2

My terminal is now writing over the same line twice. It also does not go to the end before it started to repeat the line. This is in my .bash_profile

c_reset="$(tput setaf 2)"
c_path="$(tput setaf 1)"
c_git_dirty="$(tput setaf 1)"
c_git_clean="$(tput setaf 2)"
c_white="$(tput setaf 7)"

PROMPT_COMMAND=$PROMPT_COMMAND' PS1="${c_path}\W${c_reset}$(git_prompt) :> "'

export PS1='\n\[\033[0;31m\]\W\[\033[0m\]$(git_prompt)\[\033[0m\]:> '
git_prompt ()
{
  # Is this a git directory?
  if ! git rev-parse --git-dir > /dev/null 2>&1; then
    return 0
  fi
  # Grab working branch name
  git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
  # Clean or dirty branch
  if git diff --quiet 2>/dev/null >&2; then
    git_color="${c_git_clean}"
  else
    git_color=${c_git_dirty}
  fi
   echo "${c_white}[$git_color$git_branch${c_white}]"
}

I get what this code dose. But my issue is it still writes over the same line. I have tried other wyas of doing this. using \003[0;31m type commands for setting the color.

What I am trying to do is get for it to tell when my git is dirty. Right now it writes over the same line in the terminal that I start on. It does this even with trying [ ]. Can someone tell me how to fix this and how the line with PROMPT_COMMAND works.

  • 1
    This is a common FAQ, and not really a programming question. See e.g. http://unix.stackexchange.com/questions/31642/bash-prompt-command-messing-up-scroll-history-display – tripleee Feb 06 '16 at 14:25

1 Answers1

2

The two lines

PROMPT_COMMAND=$PROMPT_COMMAND' PS1="${c_path}\W${c_reset}$(git_prompt) :> "'

export PS1='\n\[\033[0;31m\]\W\[\033[0m\]$(git_prompt)\[\033[0m\]:> '

are enough to discuss the problem: in the latter, the \[ and \] pairs surround text which you are telling bash to not count in the printable width of your prompt. But in the PROMPT_COMMAND you are resetting PS1 with similar strings (escape sequences) which bash does not know are largely non-printing characters. So bash counts the number of characters in those escape sequences as one column per character. It gets confused about how long the line (with the prompt and any command that you are editing) may be.

The fix would be to review the PROMPT_COMMAND and add those markers where appropriate, e.g., something like this (untested):

PROMPT_COMMAND=$PROMPT_COMMAND' PS1="${c_path}\W\[${c_reset}$(git_prompt)\] :> "'

Addressing the comment:

  • the export causes that value of PS1 to be put into the environment, so that for example if you run a subshell (start bash, and then run bash inside that), the value of PS1 can be used by the subshell.
  • the PROMPT_COMMAND sets PS1 each time you get a prompt.
  • in the command, $( and ) delimit commands (such as the function git_prompt), while ${ and } delimit variables (things that have a value, and unlike commands are not evaluated or executed).

Further reading:

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • I tested this and it did not work. I have a few questions. PROMPT_COMMAND=$PROMPT_COMMAND 'other-stuff' Does this set a system variable to itself plus some other stuff? – user1519219 Feb 06 '16 at 02:43
  • Also @Thomas thanks. What does Export PS1 do? Why are some var wrapped with {} and other with ()? I only understand how like half of it works. I did not want to do two posts but I git enter before I was ready.. – user1519219 Feb 06 '16 at 02:59