Search code examples
bashterminalosx-elcapitangithub-for-mac

OSX El Capitan terminal/.bash_profile color bug


I recently updated to El Capitan and I've been seeing some issues with my terminal, and I narrowed it down to my .bash_profile. I have this in my .bash_profile, so the prompt will change color based on changes in git.

# A more colorful prompt
# \[\e[0m\] resets the color to default color
c_reset='\[\e[0m\]'
# \e[0;31m\ sets the color to red
c_path='\[\e[0;31m\]'
# \e[0;32m\ sets the color to green
c_git_clean='\[\e[0;32m\]'
# \e[0;31m\ sets the color to red
c_git_dirty='\[\e[0;31m\]'

It was working with the latest update from OSX Yosemite. Also, so far as I can tell, the color codes are correct. However, this is how my terminal appears:

github.io [\[\e[0;31m\]working\[\e[0m\]]:>

As you can see, I am on my "working" branch of a github directory. Anything that is not on github appears normal.

Downloads:> 

As of right now, I've switched to iTerm which doesn't seem to have the issue for the latest version (which is updated to accomodate El Capitan). Leaving me to think it is a terminal issue rather than github.

Screenshot of Terminal


Solution

  • I found that tput setaf worked well for me. Documentation here.

    # A more colorful prompt
    # \[\e[0m\] resets the color to default color
    c_reset=$(tput setaf 0)
    #  \e[0;31m\ sets the color to purple
    c_path=$(tput setaf 55)
    # \e[0;32m\ sets the color to green
    c_git_clean=$(tput setaf 2)
    # \e[0;31m\ sets the color to red
    c_git_dirty=$(tput setaf 9)
    

    To see all the colors, I ran one of the scripts from that documentation:

    for C in {0..255}; do
        tput setaf $C
        echo -n "$C "
    done
    tput sgr0
    echo
    

    Then whatever colors you want, you know the number to insert after 'tput setaf '

    Side note: it looks like we have the same bash_profile source. I also found that upgrading to El Capitan broke it. You can also fix the path color by adding a semicolon in the middle of this line:

    # PS1 is the variable for the prompt you see everytime you hit enter
    PROMPT_COMMAND=$PROMPT_COMMAND'; PS1="${c_path}\W${c_reset}$(git_prompt) :> "'
    

    That seems to have fixed my path name color as well. :)