Every time I set up a new Linux machine for development, I frequently find myself always Googling the Bash shell modifications to make the shell prompt more Git friendly. Never again!
Git Branch Autocomplete
- Download this file https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-completion.bash to
~/.git-completion.bash
by running the following commands:- Go to your home directory
cd ~
- Download the file
wget https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-completion.bash
- Hide the file
mv git-completion.bash .git-completion.bash
- Go to your home directory
- Create a reference to the git-completion bash script on your
.bashrc
file by runningecho -e "\nsource ~/.git-completion.bash" >> ~/.bashrc
- Load the updated changes to your
~/.bashrc
file by runningsource ~/.bashrc
Show Git Branch on the Shell Prompt
- Download this file https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-prompt.sh to
~/.git-prompt.sh
by running the following commands:- Go to your home directory
cd ~
- Download the file
wget https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-prompt.sh
- Hide the file
mv git-prompt.sh .git-prompt.sh
- Go to your home directory
- Create a reference to the git-prompt shell script on your
.bashrc
file by running the commandecho -e "\n. ~/.git-prompt.sh" >> ~/.bashrc
- Modify the existing
PS1=
value in your~/.bashrc
file or inject an override of thePS1
variable.- To inject an override, run the command
echo "export PS1=\"\\[\\e[32m\\]\\u@\\h\\[\\e[00m\\]:\\[\\e[94m\\]\\w \\[\e[91m\\]\\\$(__git_ps1 \\\"(%s)\\\")\\[\\e[00m\\]\$ \"" >> ~/.bashrc
This will include the following line to your
~/.bashrc
file:export PS1="\[\e[32m\]\u@\h\[\e[00m\]:\[\e[94m\]\w \[\e[91m\]\$(__git_ps1 \"(%s)\")\[\e[00m\]$ "
This can be further customized to be formatted and output more information by adding additional parameters. (See below)
- To inject an override, run the command
- Load the updated changes to your
~/.bashrc
file by runningsource ~/.bashrc
Formatting the Prompt
Colours can be set by adding additional values to the PS1
string in \e[32m
where 32
represents one of the color codes below. This code will override the color for the rest of the prompt so if you want to change the color to something else or reset it, you will need to include either another color code or the reset code \e[0m]
at the point in the string where you want to end the current color.
Color | Foreground Code | Background Code |
---|---|---|
Black | 30 | 40 |
Red | 31 | 41 |
Green | 32 | 42 |
Yellow | 33 | 43 |
Blue | 34 | 44 |
Magenta | 35 | 45 |
Cyan | 36 | 46 |
Light Gray | 37 | 47 |
Gray | 90 | 100 |
Light Red | 91 | 101 |
Light Green | 92 | 102 |
Light Yellow | 93 | 103 |
Light Blue | 94 | 104 |
Light Magenta | 95 | 105 |
Light Cyan | 96 | 106 |
White | 97 | 107 |
Additional formatting options include:
Code | Description |
---|---|
0 | Reset/Normal |
1 | Bold text |
2 | Faint text |
3 | Italics |
4 | Underlined text |
Adding Additional Information to the Prompt
Additional parameters can be added to PS1
to include a variety of information such at the current time.
d - the date in "Weekday Month Date" format (e.g., "Tue May 26")
e - an ASCII escape character (033)
h - the hostname up to the first .
H - the full hostname
j - the number of jobs currently run in background
l - the basename of the shells 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., 4.00)
V - the release of bash, version + patch level (e.g., 4.00.0)
w - Complete path of current working directory
W - the basename of the current working directory
! - 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, which could be used to embed a terminal control sequence into the prompt
] - end a sequence of non-printing characters