Showing branch name (using colors) on windows subsystem for linux (wsl)


Hello,

The problem:

The default WSL is using bash and even though it supports coloring and customization, the default one is very simple. Since I work with git and I’d like to know which branch I am working on, I’d like to see how to show it in the bash prompt.

The solution (Please notice that I am assuming your installation is using Bash as shell, other shells may be supported, but required some variations in the instructions. )

1- As obvious that it’d sound, first install Windows Subsystem for Linux (WSL)
2- Install Git into the WSL, normally it’s about executing the following command:
sudo apt-get install git

3- Open a terminal Window, and create a file at home. It can be any name. We will use ,git-prompt.sh the content of this file can be download from https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh I tried adding the file content here, but it does not render well 😉

The if statement is optional, however we assume that the file might not be present.
5- Now if you took a look at the file, in the header there is an explanation about how to update a bash or zsh. We will be using bash since we also will support some coloring.

The original script suggest:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

However it’s possible in Bash to add support for colors. IF you want to understand better bash command and feature, type the following command in you bash terminal
man bash

Here is a sample of the updated PS1
PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]$(__git_ps1 " (%s)")\[\033[0m\]\n$'

The final result will show, the

username@hotname directory (branch)

in the bash promt

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.