On my first day or two at work, the developer I was pairing with sent me their Bash profile, which I copied, pasted, and forgot. Whenever I cracked open my Bash profile to add a Git alias or two, I would look at the line that describes my Bash command prompt and wince in terror. I rarely changed or thought about my command prompt. Instead, I was careful not to touch it for fear of making some accidental and fatal change.
export PS1='\[\033[01;36m\]\u@\[\033[01;32m\]\h:\[\033[33;1m\]\w\[\033[01;31m\]$(__git_branch)\n\[\e[01;33m\]] \[\e[00m\]'
As with most things, once you break it down, it is not that complicated. Here are a few tools to help understand what this garbled mess of numbers and letters means and how to spice it up just how you want it.
1. Master the Basics
When you take away all the other complexity, you can see the basic building blocks of the prompt. In this simple example, the user name, host name, and full path to the current working directory are displayed.
- \u – User name
- \h – Host name
- \w – Path of the current working directory
2. Show Git Branch Information
You can add information about your Git branch to add functionality to your command prompt. Having a quick way to see your current branch will save you time and lots of tears.
alias __git_branch="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'" export PS1='\u@\h:\w $(__git_branch)'
3. Tell Time
Use the built-in PS1 codes to include time in your prompt. The following two examples include time in either a 12- or 24-hour format.
export PS1='[\t] \u@\h:\w $(__git_ps1)' export PS1='[\@] \u@\h:\w $(__git_ps1)'
- \t – Time in 24 hour format
- \@ – Time in 12 hour format
4. Use Foreground Colors
Now, let’s add in some more spice. Colors help to differentiate various parts of the prompt and are much more interesting to see. Here are two examples with varying degrees of colorization. The first applies the color to the entire prompt, and the second colors individual portions.
export PS1='\e[0;35m[\t] \u@\h:\w $(__git_ps1)\e[m' export PS1='\e[1;35m[\t]\e[m \e[1;36m\u@\e[m\e[1;32m\h:\e[m\e[33;1m\w\e[m \e[1;31m$(__git_ps1)\e[m\n\[\e[01;33m\]] \[\e[00m\]'
- \e[ – Beginning of color prompt
- x;ym (e.g. 0;33m) – Indicates color code. See color codes below.
- \e[m – End of color prompt
Foreground Color Codes: Black 0;30 Red 0;31 Green 0;32 Brown 0;33 Blue 0;34 Purple 0;35 Cyan 0;36 Light Gray 0;37 [Tip: Use 1 instead of 0 for light color]
Read more about finding your perfect colors here.
5. Add Background Colors
If you want to add even more color, it is possible to define background colors with the same syntax outlined above using background color codes. The examples show one background for the entire prompt and for only a portion.
export PS1='\e[41m[\t] \u@\h:\w $(__git_ps1)\e[m' export PS1='\e[1;35m[\t]\e[m \e[1;36m\u@\e[m\e[1;32m\h:\e[m\e[33;1m\w\e[m \e[1;41m$(__git_ps1)\e[m\n\[\e[01;33m\]] \[\e[00m\]'
Background Color Codes: Black 0;40 Red 0;41 Green 0;42 Brown 0;43 Blue 0;44 Purple 0;45 Cyan 0;46 White 0;47 [Tip: Use 1 instead of 0 for light color]
I have only scratched the surface of functionalities you can include in your prompt. With these tools, you’ll be able to delve deeper and customize your prompt any way you like it.