Skip to content

Instantly share code, notes, and snippets.

@adojos
Last active March 14, 2024 17:04
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adojos/fd30baa716dbfbe282fe147ab717b664 to your computer and use it in GitHub Desktop.
Save adojos/fd30baa716dbfbe282fe147ab717b664 to your computer and use it in GitHub Desktop.
Git: Customizing GitBash Shell Prompt #gitbash

Customizing GitBash Shell Prompt and Theme


As an example, lets assume we want to change / customize the following :

  • Change the Title of the GitBash window from default [MINGW64:/directoryName] to [Tushars Gitbash (Win64)]
  • Change the default prompt () to a customized prompt e.g [GitBash (Win64):> ]

👉 Note ($HOME/.bash_profile vs $HOME/.bashrc vs $HOME/.config):

The below described method only focusses on customizing the 'GitBash' prompt and NOT about customizing default 'Bash' prompt on Linux terminals. Hence this method uses the '$HOME/.config' folder for storing 'GitBash' prompt customization as per XDG Directory Specs. If you are looking for customizing default 'Bash' prompt for your Linux terminal you should be using '$HOME/.bashrc' for storing your customization and hence advised not to continue with this method.


1. Locating Default 'git-prompt.sh' File

By default the GitBash prompt settings / configuration come from shell script called 'git-prompt.sh'. This is usually hosted inside 'profile.d' directory inside the GitBash installation directory. Navigate to the installation path on your system for GitBash. On windows, this is generally found in the following directory :

C:\Program Files\Git\etc\profile.d\

Editing Default 'git-prompt.sh' file Vs User Specific Git Prompt Config

For obvious reasons, its recommended NOT to change the default installation files and rather go for user customization via user specific config files.


2. Locate User Home Directory

On windows, generally the user home directory is "C:/Users/username/".

You can also use the following commands to find your home direcotry :

eval echo ~$USER
echo $HOME

3. Locate or Create User Specific Git Prompt Config Folders / Files

Check if the following path (folders) exist in the users home directory, If the directories don't exist then create them :

*.config
* git

After above step, you should have the following directory path by now : "C:\Users\UserName\.config\git\". Next create a file named 'git-prompt.sh' and place it inside the 'git' direcotry created / located above.

Finally you should have the full path as "C:\Users\UserName\.config\git\git-prompt.sh"

~C:\Users\UserName\
		|-- .config
			|-- git
				|-- git-prompt.sh

4. List of Special Characters for PS1

Bash allows prompt strings to be customized by inserting following backslash-escaped special characters:

\a : an ASCII bell character (07)
\d : the date in “Weekday Month Date” format (e.g., “Wed Aug 28”)
\D{format} : an empty format results in a locale-specific time representation. The braces are required
\e : an ASCII escape character (033)
\h : the hostname up to the first ‘.’
\H : the hostname
\j : the number of jobs currently managed by the shell
\l : the basename of the shell’s terminal device name
\n : newline
\r : carriage return
\s : the name of the shell, the basename of $0 (the portion following the final slash)
\t : the current time in 24-hour HH:MM:SS format
\T : the current time in 12-hour HH:MM:SS format
\@ : the current time in 12-hour am/pm format
\A : the current time in 24-hour HH:MM format
\u : the username of the current user
\v : the version of bash (e.g., 2.00)
\V : the release of bash, version + patch level (e.g., 2.00.0)
\w : the current working directory, with $HOME abbreviated with a tilde
\W : the basename of the current working directory, with $HOME abbreviated with a tilde
\! : the history number of this command
\# : the command number of this command
\$ : if the effective UID is 0, a #, otherwise a $
\nnn : the character corresponding to the octal number nnn
\\ : a backslash
\[ : begin a sequence of non-printing characters
\] : end a sequence of non-printing characters


5. PS1 Construct & Adding Colors

To add colors to the shell prompt use the following export command syntax:

'\e[x;ym $PS1 \e[m'

Where,

\e[   : Start color scheme. (You can also use '\033[' in place of '\e[')
x;y   : Color pair to use (x;y)
$PS1  : Your shell prompt variable.
\e[0m : Stop color scheme. (You can also use '\033[0m' in place of '\e[0m')

When using 16 Color Scheme, follow below syntax

Grey Background + Black Text = '[\e[30;47m] Hello World\e[0m'


When using 256 Color Scheme, follow below syntax

Foreground (text):

For using one of the 256 colors on the foreground (text color), the control sequence is “[38;5;ColorNumberm” where ColorNumber is one of the color code from 256 scheme (see Color Codes section under Additional Resources below)

Background:

For using one of the 256 colors on the background, the control sequence is “[48;5;ColorNumberm” where ColorNumber is one of the color code from 256 scheme (see Color Codes section under Additional Resources below)

Example: 256 Color Only

Default Background + Green Text(256) + Red Text(256) = '\e[38;5;82mHello \e[38;5;198mWorld\e[0m'

Example: Combination of 16 and 256 Color

(Black Background(16) + Green Text(256)) + (Green Background(16) + Black Text(256)) = '\e[40;38;5;82m Hello \e[30;48;5;82m World \e[0m'


6. Preparing User Specific Git Prompt Config File

Open the default 'git_prompt.sh' at C:\Program Files\Git\etc\profile.d\ to view the contents. You need to basically customize the value of PS1 string and then copy the modified script into user specific prompt config which we created above.

Note : You can visit the links mentioned in the 'Reference Links' section below for further reading on customization.

In order to implement our requirement here we shall be changing the GitBash window title and the prompt. For doing so we shall be copying the below construct 'as-is' into the user specific prompt config script file which we created above.

I shall be sharing two versions of custom 'git-prompt.sh', please use any one of these for your configuration.

Copy the below contruct 'As-Is' and save it into your user specific config script file located here : "C:\Users\UserName\.config\git\git-prompt.sh" The changes made have been commented as 'CHANGED HERE'

Given below is very basic, custom version of 'git-prompt.sh' which uses just static (hard-coded) text to replace the default title and prompt

	PS1='\[\033]0;Tushars GitBash (Win64): \W\007\]' # set window title	# CHANGED HERE [added custom String]
	PS1="$PS1"'\n'                 			# new line
	PS1="$PS1"'\[\033[32m\]'       			# change to green
	PS1="$PS1"'GitBash (Win64):> '            	# user@host<space>	# CHANGED HERE [changed to custom String]
	#PS1="$PS1"'\[\033[35m\]'       		# change to purple	# CHANGED HERE  ['removed MINGW64' by commenting out]
	#PS1="$PS1"'$MSYSTEM '          # show MSYSTEM		# CHANGED HERE  ['removed MINGW64' by commenting out]
	PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
	PS1="$PS1"'\W'                 # current working directory		# CHANGE HERE  [changed to Dir Basename]
	if test -z "$WINELOADERNOEXEC"
	then
		GIT_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 cyan
			PS1="$PS1"'`__git_ps1`'   # bash function
		fi
	fi
	PS1="$PS1"'\[\033[0m\]'        # change color
	PS1="$PS1"'\n'                 # new line
	PS1="$PS1"'$ '                 # prompt: always $
	
MSYS2_PS1="$PS1"               # for detection by MSYS2 SDK's bash.basrc

# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
	for c in "$HOME"/bash_completion.d/*.bash
	do
		# Handle absence of any scripts (or the folder) gracefully
		test ! -f "$c" ||
		. "$c"
	done
fi

Below is another version of the same 'git-prompt.sh' but includes (a) color customization and (b) git branch function along with mostly automated switches rather then static (hard-coded) text:

	PS1='\[\033]0; Git | Bash v\v | \h (Win64) | \W\007\]' 		# set window title
	PS1="$PS1"'\n'							# new line
	PS1="$PS1"'\[\e[30;47m\] [\A] '					# insert real-time clock
	PS1="$PS1"'\[\e[97;104m\] \u '					# insert user
	PS1="$PS1"'\[\e[30;46m\] (@\h) '				# insert @host
	PS1="$PS1"'\[\e[30;43m\] <\W> '					# insert current working directory
	
	if test -z "$WINELOADERNOEXEC"
	then
		GIT_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"'\[\e[97;45m\]'  # change color to cyan
			PS1="$PS1"'`__git_ps1` '   # bash function
		fi
	fi
	PS1="$PS1"'\[\033[0m\]'        # change color
	PS1="$PS1"'\n'                 # new line
	PS1="$PS1"'$ '                 # prompt: always $

MSYS2_PS1="$PS1"               # for detection by MSYS2 SDK's bash.basrc

# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
	for c in "$HOME"/bash_completion.d/*.bash
	do
		# Handle absence of any scripts (or the folder) gracefully
		test ! -f "$c" ||
		. "$c"
	done
fi

7. Verify Your Changes

After the above procedures, you can verify your changes by re-launching the GitBash. If your changes did not take effect, visit the 'Reference Links' section for further reading.

Note :

If you encounter any errors or issues after above changes, you can always revert back to default configuration by deleting the above files and folders that you created.


8. Download and Install Additional Base16 Themes (Mintty)

After implementing the above steps (1 to 5) for customizing the PS1 prompt, you should have desired customized command promopt and window title for your GitBash. Now it is time to download some additional Base16 themes for your GitBash terminal!

Download and Install Mintty Themes

  1. Navigate to user home directory on windows (e.g C:\Users\Username)
  2. Create new folder called '.mintty' inside the user home directory.
  3. Inside the '.mintty' folder, create another folder called 'themes'. The complete path should look like (C:\Users\Username\.mintty\themes)
  4. Now download the additional Mintty themes from here
  5. Unzip the downloaded repository and copy all the theme files from 'mintty' folder into your 'themes' folder created in step3 above.
  6. Now re-launch the GitBash terminal.
  7. Open-up the GitBash 'Options' by clicking on the left-hand side top corner of the terminal window.
  8. Now you can select any desired theme from 200+ mintty themes.

Some of my favourite Mintty themes:

  1. base16-harmonic-dark-256.minttyrc
  2. base16-helios-256.minttyrc
  3. base16-hopscotch-256.minttyrc
  4. base16-horizon-dark-256.minttyrc

9. Try Dev fonts for your terminal

Choose any of the Monospaced fonts from the below list:

  1. Microsoft Cascadia
  2. Meslo (Nerd Font)
  3. Fira Code
  4. Roboto Mono

And you are all set! Enjoy your new terminal!


Additional Resources


Color Codes (8/16)

Foreground (text) Color Background Color
Code Color Code Color
39 Default foreground color 49 Default background color
30 Black 40 Black
31 Red 41 Red
32 Green 42 Green
33 Yellow 43 Yellow
34 Blue 44 Blue
35 Magenta 45 Magenta
36 Cyan 46 Cyan
37 Light gray 47 Light gray
90 Dark gray 100 Dark gray
91 Light red 101 Light red
92 Light green 102 Light green
93 Light yellow 103 Light yellow
94 Light blue 104 Light blue
95 Light magenta 105 Light magenta
96 Light cyan 106 Light cyan
97 White 107 White

Color Codes (256)


Foreground

256 Colors


Background

256 Colors


Reference Links

Bash Manual

Bash Prompt Escape Sequences

Bash tips: Colors and formatting

How-To Customize GitBash Videos on Youtube

Online PS1 Prompt Generator

Base16 Mintty Themes

@noodlescripter
Copy link

Thanks a lot, this works like a charm!!

@adojos
Copy link
Author

adojos commented Nov 25, 2022

Glad to hear that! You are welcome. 👍

@VoidPaul
Copy link

Finally customized my Git Bash prompt. Without this, I would've had to tolerate the default color scheme.

@adojos
Copy link
Author

adojos commented Jul 31, 2023

Cool 👍

@tharphyolinn
Copy link

──(user㉿kali)-[~]
└─$ sudo su
[sudo] password for user:
kali#

can u help me with this root color is not changing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment