Customizing Git Bash in Windows
5/21/2021#bash #git #tutorial
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!
Finding and Editing git-prompt
Open your git bash terminal and type this commands:
1cd /etc/profile.d/ 2explorer .
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.
1if test -f /etc/profile.d/git-sdk.sh 2then 3 TITLEPREFIX=SDK-${MSYSTEM#MINGW} 4else 5 TITLEPREFIX=$MSYSTEM 6fi 7 8if test -f ~/.config/git/git-prompt.sh 9then 10 . ~/.config/git/git-prompt.sh 11else 12 PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title 13 PS1="$PS1"'\n' # new line 14 PS1="$PS1"'\[\033[32m\]' # change to green 15 PS1="$PS1"'\u@\h ' # user@host<space> 16 PS1="$PS1"'\[\033[35m\]' # change to purple 17 PS1="$PS1"'$MSYSTEM ' # show MSYSTEM 18 PS1="$PS1"'\[\033[33m\]' # change to brownish yellow 19 PS1="$PS1"'\w' # current working directory 20 if test -z "$WINELOADERNOEXEC" 21 then 22 GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)" 23 COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}" 24 COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}" 25 COMPLETION_PATH="$COMPLETION_PATH/share/git/completion" 26 if test -f "$COMPLETION_PATH/git-prompt.sh" 27 then 28 . "$COMPLETION_PATH/git-completion.bash" 29 . "$COMPLETION_PATH/git-prompt.sh" 30 PS1="$PS1"'\[\033[36m\]' # change color to cyan 31 PS1="$PS1"'`__git_ps1`' # bash function 32 fi 33 fi 34 PS1="$PS1"'\[\033[0m\]' # change color 35 PS1="$PS1"'\n' # new line 36 PS1="$PS1"'$ ' # prompt: always $ 37fi 38 39MSYS2_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:
1 PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title 2 PS1="$PS1"'\n' # new line 3 PS1="$PS1"'\[\033[32m\]' # change to green 4 PS1="$PS1"'\u@\h ' # user@host<space> 5 PS1="$PS1"'\[\033[35m\]' # change to purple 6 PS1="$PS1"'$MSYSTEM ' # show MSYSTEM 7 PS1="$PS1"'\[\033[33m\]' # change to brownish yellow 8 PS1="$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:
1PS1="$PS1"'\[\033[32m\]' # change to green 2PS1="$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:
1Black 30 2Red 31 3Green 32 4Brown/Orange 33 5Blue 34 6Purple 35 7Cyan 36 8Light Gray 37
There are some special codes that change the style of the text too:
1Normal 0 2Bold text 1 3Faint text 2 4Italic 3 5Underlined 4
So we can make some pretty cool customization combining those together, here is an example:
1PS1="$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
1PS1="$PS1"'\[\033[1;33m\]' # change color to bold yellow 2PS1="$PS1"'`__git_ps1`' # bash function
And this ones defines the second line, where you write the commands (in case is only the "$")
1PS1="$PS1"'\[\033[1;31m\]' # change color to bold red 2PS1="$PS1"'\n' # new line 3PS1="$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:
1if test -f /etc/profile.d/git-sdk.sh 2then 3 TITLEPREFIX=SDK-${MSYSTEM#MINGW} 4else 5 TITLEPREFIX=$MSYSTEM 6fi 7 8if test -f ~/.config/git/git-prompt.sh 9then 10 . ~/.config/git/git-prompt.sh 11else 12 PS1='\[\033]0;Terminal Dir:$PWD\007\]' # set window title 13 PS1="$PS1"'\n' # new line 14 PS1="$PS1"'\[\033[1;35m\]' # change to bold purple 15 PS1="$PS1"'GMkonan ' # user@host<space> 16 PS1="$PS1"'\[\033[1;32m\]' # change to bold green 17 PS1="$PS1"'in ' # show MSYSTEM 18 PS1="$PS1"'\[\033[1;36m\]' # change to bold cyan 19 PS1="$PS1"'\W' # directory without the path 20 if test -z "$WINELOADERNOEXEC" 21 then 22 GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)" 23 COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}" 24 COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}" 25 COMPLETION_PATH="$COMPLETION_PATH/share/git/completion" 26 if test -f "$COMPLETION_PATH/git-prompt.sh" 27 then 28 . "$COMPLETION_PATH/git-completion.bash" 29 . "$COMPLETION_PATH/git-prompt.sh" 30 PS1="$PS1"'\[\033[1;33m\]' # change color to bold yellow 31 PS1="$PS1"'`__git_ps1`' # bash function 32 fi 33 fi 34 PS1="$PS1"'\[\033[1;31m\]' # change color to bold red 35 PS1="$PS1"'\n' # new line 36 PS1="$PS1"'$ ' # prompt: always $ 37fi 38 39MSYS2_PS1="$PS1" # for detection by MSYS2 SDK's bash.basrc 40 41# Evaluate all user-specific Bash completion scripts (if any) 42if test -z "$WINELOADERNOEXEC" 43then 44 for c in "$HOME"/bash_completion.d/*.bash 45 do 46 # Handle absence of any scripts (or the folder) gracefully 47 test ! -f "$c" || 48 . "$c" 49 done 50fi 51 52MSYS2_PS1="$PS1" # for detection by MSYS2 SDK's bash.basrc