Using Linux on Windows (Git Bash & Windows Terminal)

Using Linux on Windows (Git Bash & Windows Terminal)

In this post, I will explain how to customize the Linux bash prompt on Windows and add it to Windows Terminal. There are several ways to use Linux on a Windows computer. The most popular methods are Git Bash and Windows Subsystem for Linux (a.k.a. WSL). I will assume that you already know how to install your own Linux on Windows.

You probably want to change your prompt for several reasons. One reason is that the default prompt is lengthy and not meaningful. You may wish to have a more clean prompt. Another reason is that the default prompt displays some private information like your user name and computer name. Because you share your screenshots online a lot, you may want to hide that information by using a more generic one or omitting it altogether.

Customize the prompt of Git Bash in Windows Terminal

The easiest method of using Linux on Windows is using Git Bash. Windows users can download it here, https://git-scm.com/download/win.
Linux terminal has a default prompt setup, which you may want to shorten for clarity. For example, there is a long user and hostname part, which are private. The entire prompt can be longer if you are working on a deeper subdirectory.

Our goal is to make the prompt short and private like this.

Steps:

  1. Go to Program Files\Git\etc\profile.d\ folder (your installation location of Git Bash)
  2. Copy the file, git-prompt.sh
  3. Move the file to your user directory, C:\Users\Seong\.config\git\ folder (replace my username with yours)
  4. Replace the content of the copied/moved file using this code block.

You can also check out a demo video on YouTube.

PS1='\[\033]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}\007\]' # set window title
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'\[\033[32m\]'       # change to green
PS1="$PS1"'bash@mingw64 '      # user@host<space> => you can be creative here!!! 
PS1="$PS1"'\[\033[35m\]'       # change to purple
# PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
PS1="$PS1"'\w'                 # 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"'\[\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 $

In my setup, I configure user and hostname as bash@mingw64 with green color, current location with yellow, git branch with cyan. And I made a new line before the prompt command ($) to write a command in the second line.

For customizing options for color and attributes, I refer to this site, https://ss64.com/bash/syntax-prompt.html. You can google more resources using keywords such as “bash prompt variables.” After finishing the prompt customization, you have to reopen Git Bash to check the change.

One more thing that you can do is to add Git Bash to Windows Terminal. Windows Terminal is an awesome tool. You can open multiple consoles (Command Prompt, PowerShell, WSL, Git Bash, etc.) simultaneously on different tabs or panes. You can stylize the look-and-feel. To add Git Bash to Windows Terminal, open Settings and copy this code block inside the list property.

{
	"guid": "{f51c639e-3765-4db9-86db-dba5e07c4622}",
	"name": "Git Bash",
	"commandline": "\"%PROGRAMFILES%\\Git\\usr\\bin\\bash.exe\" --login -i -l",
	"icon": "%PROGRAMFILES%\\Git\\mingw64\\share\\git\\git-for-windows.ico",
	"startingDirectory": "~",
	"hidden": false
}

To make your profile as default, you have to define a unique GUID. You can create a guid using PowerShell with the command NEW-GUID. You can set up your default profile by replacing the defaultProfile guid in the settings.json file.

And restart Windows Terminal. You will find that the Git Bash is loaded right away instead of PowerShell. For more customization of Windows Terminal, you can refer to this official page, https://docs.microsoft.com/en-us/windows/terminal/customize-settings/profile-settings.