Bash Prompt Not Showing Colours (git)

06
2013-12
  • michaelward82

    I have the following in my .bash_profile, to give me git branch info in my bash prompt:

    # Configure colors, if available.
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
      c_reset='\[\e[0m\]'
      c_user='\[\033[1;33m\]'
      c_path='\[\e[0;33m\]'
      c_git_clean='\[\e[0;36m\]'
      c_git_dirty='\[\e[0;35m\]'
    else
      c_reset=
      c_user=
      c_path=
      c_git_clean=
      c_git_dirty=
    fi
    
    # Function to assemble the Git part of our prompt.
    git_prompt ()
    {
      if ! git rev-parse --git-dir > /dev/null 2>&1; then
        return 0
      fi
    
      git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
    
      if git diff --quiet 2>/dev/null >&2; then
        git_color="$c_git_clean"
      else
        git_color="$c_git_dirty"
      fi
    
      echo " [$git_color$git_branch${c_reset}]"
    }
    
    # Thy holy prompt.
    PROMPT_COMMAND='PS1="${c_user}\u${c_reset}@${c_user}\h${c_reset}:${c_path}\w${c_reset}$(git_prompt)\$ "'
    

    Everything is working great apart from the colours. If I remove the if statement around the colour definitions (and remove the else section) then I do get the colours.

    I'm new to bash, I have little concept of what the if statement does and why it is not evaluating to true.

    I'm happy to go without the if statement, but I assume it's there for a reason and until I know why then I don't want to cause myself unknown hassles.

    I am running FreeBSD 9.0, and Bash 4.1.9(0)-release

  • Answers
  • slhck

    According to the manpage tput returns 1 if the terminal has the attribute.

    So I think the check is inverted.


  • Related Question

    why zsh git autocompletion is not as good as bash?
  • Vitaly Kushner

    zsh is recommended all over the place and is supposed to be one of the more advanced shells out there. Why then I find it that zsh autocompletion lacking in many areas that I'm used to with bash?

    in particular git autocompletion in zsh is very bad:

    • in bash it knows to complete branches and command options. not so in zsh
    • in zsh it doesn't even complete filenames after many 'git' commands (for example git diff). duh!

  • Related Answers
  • a paid nerd

    Try upgrading to at least Zsh 4.3.10. It fixes the problems with Git aliases.

  • SvW

    Autocompletion for git is not a bash-builtin functionality, but an additional "third-party plugin" using the basic autocomplete system of the shell, which you happen to have installed (AFAIK it is a part of the git install).

    For zsh there either exists no similar autocomplete file or you don't have it installed.

    Nevertheless, this says nothing about the quality of either shell, it just means that someone took the effort to create this for the bash, but for zsh no one wanted to (or you just don't have it installed).

  • wRAR

    You are wrong, zsh does complete branches, options and files, at least in ALT Linux and Debian.

  • sp3ctum

    I'm a really new user of zsh but one of the first things I installed was oh-my-zsh. It is a community-driven collection of plugins, themes and other awesome stuff. It has a solid set of default settings that have "just worked" for me.

    It makes for a fun experience since with zsh's awesome completion system managing local and remote branches is a lot clearer.

    My version is from the standard Ubuntu repositories: zsh 4.3.11 (x86_64-unknown-linux-gnu). I suggest you try oh-my-zsh out to see if it works for you out of the box like it did for me.