Hello guys, this is gonna be a small post about git bash customization, so by the end of it you will know how to change some aspects of your git terminal, for example making it like mine:
so let's dive in!
Open your git bash terminal and type this commands:
cd /etc/profile.d/explorer .
The first command is to change your current directory to the /etc/profile.d/
that is where the file we need to edit is. The other command just open the explorer in the current directory, so you will se something like this:
Here is our git-prompt.sh. Now we can open it and see what we have there.
if test -f /etc/profile.d/git-sdk.shthenTITLEPREFIX=SDK-${MSYSTEM#MINGW}elseTITLEPREFIX=$MSYSTEMfiif test -f ~/.config/git/git-prompt.shthen. ~/.config/git/git-prompt.shelsePS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window titlePS1="$PS1"'\n' # new linePS1="$PS1"'\[\033[32m\]' # change to greenPS1="$PS1"'\u@\h ' # user@host<space>PS1="$PS1"'\[\033[35m\]' # change to purplePS1="$PS1"'$MSYSTEM ' # show MSYSTEMPS1="$PS1"'\[\033[33m\]' # change to brownish yellowPS1="$PS1"'\w' # current working directoryif test -z "$WINELOADERNOEXEC"thenGIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"if test -f "$COMPLETION_PATH/git-prompt.sh"then. "$COMPLETION_PATH/git-completion.bash". "$COMPLETION_PATH/git-prompt.sh"PS1="$PS1"'\[\033[36m\]' # change color to cyanPS1="$PS1"'`__git_ps1`' # bash functionfifiPS1="$PS1"'\[\033[0m\]' # change colorPS1="$PS1"'\n' # new linePS1="$PS1"'$ ' # prompt: always $fiMSYS2_PS1="$PS1" # for detection by MSYS2 SDK's bash.basrc
Here we have a lot of different things, but let's focus on the important part, this one:
PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window titlePS1="$PS1"'\n' # new linePS1="$PS1"'\[\033[32m\]' # change to greenPS1="$PS1"'\u@\h ' # user@host<space>PS1="$PS1"'\[\033[35m\]' # change to purplePS1="$PS1"'$MSYSTEM ' # show MSYSTEMPS1="$PS1"'\[\033[33m\]' # change to brownish yellowPS1="$PS1"'\w' # current working directory
Here is where we are gonna customize it. The nice thing about git bash, is that there are already some pretty comments that help us understand what each thing does.
To customize it we are gonna need to understand two things: The special backslash characters and the ANSI color escape codes.
The first one is pretty simple, there are some of them and they each represent something that can be seeing in our terminal, here are some of them:
\u => username
\h => hostname until the first '.'
\w => current working directory
So for example the line PS1="$PS1"'\u@\h'
Would be username@hostname
.
The last thing is the color codes. Those are used before the line we want to color it, so:
PS1="$PS1"'\[\033[32m\]' # change to greenPS1="$PS1"'\u@\h ' # user@host<space>
Here we are changing the username@hostname
to green. There are a few color codes we can use in bash such as:
Black 30Red 31Green 32Brown/Orange 33Blue 34Purple 35Cyan 36Light Gray 37
There are some special codes that change the style of the text too:
Normal 0Bold text 1Faint text 2Italic 3Underlined 4
So we can make some pretty cool customization combining those together, here is an example:
PS1="$PS1"'\[\033[1;36m\]' # change to bold 1() and cyan (36)
There are some other lines that you can customize. Basically every line that is changing PS1 represents a customization for some part, and all of them have good comments to guide you. I only focused in the first ones to be easier to understand, since you are focusing in only a couple lines, instead of the entire file. But if you prefer that I point out them, they are these lines here:
This one changes the part that indicates in which branch you are
PS1="$PS1"'\[\033[1;33m\]' # change color to bold yellowPS1="$PS1"'`__git_ps1`' # bash function
And this ones defines the second line, where you write the commands (in case is only the "$")
PS1="$PS1"'\[\033[1;31m\]' # change color to bold redPS1="$PS1"'\n' # new linePS1="$PS1"'$ ' # prompt: always $
And that's it, with this knowledge you can make some more customizations in your git bash :). If you liked how mine feel free to get the code for you here:
if test -f /etc/profile.d/git-sdk.shthenTITLEPREFIX=SDK-${MSYSTEM#MINGW}elseTITLEPREFIX=$MSYSTEMfiif test -f ~/.config/git/git-prompt.shthen. ~/.config/git/git-prompt.shelsePS1='\[\033]0;Terminal Dir:$PWD\007\]' # set window titlePS1="$PS1"'\n' # new linePS1="$PS1"'\[\033[1;35m\]' # change to bold purplePS1="$PS1"'GMkonan ' # user@host<space>PS1="$PS1"'\[\033[1;32m\]' # change to bold greenPS1="$PS1"'in ' # show MSYSTEMPS1="$PS1"'\[\033[1;36m\]' # change to bold cyanPS1="$PS1"'\W' # directory without the pathif test -z "$WINELOADERNOEXEC"thenGIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"if test -f "$COMPLETION_PATH/git-prompt.sh"then. "$COMPLETION_PATH/git-completion.bash". "$COMPLETION_PATH/git-prompt.sh"PS1="$PS1"'\[\033[1;33m\]' # change color to bold yellowPS1="$PS1"'`__git_ps1`' # bash functionfifiPS1="$PS1"'\[\033[1;31m\]' # change color to bold redPS1="$PS1"'\n' # new linePS1="$PS1"'$ ' # prompt: always $fiMSYS2_PS1="$PS1" # for detection by MSYS2 SDK's bash.basrc# Evaluate all user-specific Bash completion scripts (if any)if test -z "$WINELOADERNOEXEC"thenfor c in "$HOME"/bash_completion.d/*.bashdo# Handle absence of any scripts (or the folder) gracefullytest ! -f "$c" ||. "$c"donefiMSYS2_PS1="$PS1" # for detection by MSYS2 SDK's bash.basrc