Setting colors for prompt in Git Bash on Windows
Asked Answered
I

2

24

I've successfully played around with some of the color settings in the Git Bash on Windows - I'm able to set a few things, like the colors of the local, the current and remote branches in my .gitconfig file:

[color "branch"]
current = cyan bold
local = cyan 
remote = red

But what I haven't managed to change are the colors of the prompt - the username@machine at the beginning of the line (in the yellow rectangle in my screenshot), and the project and branch I'm currently on (purple rectangle).

enter image description here

Is there a way to influence those, too? Which .gitconfig settings do I need to set to change those colors?

Inbreed answered 7/5, 2012 at 11:59 Comment(5)
Do you have a ~/.bashrc or ~/.bash_profile? What happens if you say PS1=""?Vogeley
I have a .bashrc file with a few bash alias - but no bash_profileInbreed
@bitmask: adding a SET PS1="" line to .bashrc doesn't appear to do anything at all ...Inbreed
In normal bash you can control the prompt solely by specifying it in the PS1 variable, so if you override it "" from the command line, the prompt should disappear (confirming that you can design it, by changing your PS1 environment variable).Vogeley
Just ask google for PS1 bash. It gives you tons of tutorials and alike.Vogeley
H
15

In your .bashrc you can set your prompt using the PS1 variable (which is likely set to a global value in /etc/profile or another file in /etc which may be distribution dependent).

Here's an example:

PS1='\[\033[1;36m\]\u@\h:\[\033[0m\]\[\033[1;34m\]\w\[\033[0m\] \[\033[1;32m\]$(__git_ps1)\[\033[0m\]\$ '

In order for the command substitution to work, you need shopt -s promptvars which is the default.

This will output the user and hostname in cyan, the current directory in blue and the git branch in green on terminals that work with TERM=xterm-color.

See man 5 terminfo and man tput for more information about terminal controls.

Howes answered 7/5, 2012 at 13:46 Comment(2)
Great ! Thanks - you learn something new every day here on Stackoverflow!Inbreed
I'm not sure of the exact rules in all situations (interactive or not) but you may need to create a .bash_profile which then calls the .bashrc with this line: [[ -f ~/.bashrc ]] && . ~/.bashrcBowers
A
0

I recently discovered, that in .bashrc file located in your user folder, there is part of script:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

that when uncommented, will add to PS1 variable the bit of colors:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

otherwise, plain prompt is set.

In general, I'd suggest to see what's happening in this script, as it's a final configuration step in launching Git Bash.

And extra tip, to apply changes without opening and closing the terminal, you can:

source ~/.bashrc
#same as
. ~/.bashrc
Amphi answered 14/9, 2023 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.