Today’s tip is on customizing the terminal prompt. If you’re a developer, there is a high chance that you use source control. There’s also a high chance that source control is git. You likely find yourself in and out of project directories, navigating different branches quite often. One customization I like to make to my prompt is adding the git branch I’m currently working in, if one exists. This is far from the only customization that can be made, but I’m going to cover because it is so common and relatable to many.

Understanding the Default Prompt

The default prompt usually includes your username, hostname, and current directory. It might look like:

user@hostname:~/current_directory$

The cool thing is you can set this to whatever you’d like - or nothing at all. Some of those things might be an indicator that shows whether you are working on a remote or local machine, cpu load and other system stats, or the time. If you want to pull the water temperature from a buoy in the Gulf of Mexico and you’d find that useful in your prompt, you can add that, too. You can also stylize your prompt with colors. You can even create a multi-line prompt with some of these on each line using the new line escape sequence (detailed below).

The prompt is simply the value of the PS1 environment variable. To view your current one, you can run echo $PS1

Changing the Prompt Temporarily:

You can change your prompt temporarily by assigning a new value to the PS1 environment variable:

PS1="MyFirstCustomPrompt$ "

This will change your prompt to MyFirstCustomPrompt$ until you close the terminal.

Customizing with Escape Sequences:

You can use escape sequences to add color, time, date, and more to your prompt. Here are the escape sequences:

  • \u: Current username

  • \h: Hostname

  • \w: Full current working directory

  • \W: Last part of the current working directory

  • \d: Date (e.g., Fri Aug 11)

  • \t: Time (24-hour format, 14:30:00)

  • \n: New line

  • \e[x;ym: This sequence is used to set foreground and background colors. Replace x and y with color codes from below.

Color Codes

  • 0 : Reset / No color
  • 1: Bold / Bright
  • 2: Dim / Faint
  • 3: Italic
  • 4: Underlined
  • 5: Blinking
  • 7: Inverted colors (swap foreground and background)
  • 8: Hidden / Concealed

Foreground Color Codes:

  • 30: Black
  • 31: Red
  • 32: Green
  • 33: Yellow
  • 34: Blue
  • 35: Magenta
  • 36: Cyan
  • 37: White

Background Color Codes:

  • 40: Black
  • 41: Red
  • 42: Green
  • 43: Yellow
  • 44: Blue
  • 45: Magenta
  • 46: Cyan
  • 47: White

For example:

PS1="[\e[1;32;41m\u@\h \W\e[m]$ "

This displays your prompt in green on a red background (ugly), including the username (\u), hostname (\h), and current directory (\W).

Git Branch Function and Make Changes Permanent:

To make your custom prompt permanent, add the PS1 assignment to your shell’s profile configuration file (~/.bashrc for Bash, ~/.zshrc for Zsh).

To add the current git branch to the prompt, you’ll need to include a function that parses the git branch for the current directory. If there is a git branch, the function returns the value to be displayed. This is what you’d add at the end of your configuration file.

parse_git_branch() {
   git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

PS1="\[\e[1;32m\][\u@\h \W\$(parse_git_branch)]\[\e[m\]$ "

This code defines parse_git_branch that extracts the current branch from a Git repository and adds the branch information to the prompt.

Conclusion

Using this method, there are all sorts of creative prompts that can be created. If you use or know of any handy prompts or come up with any after reading this, please share in the comments.