Today I learned… that your Mac terminal bash prompt can be customized and enhanced well beyond its basic boring default.
A full tour of bash customizations is outside the scope of this post, but I do want to share the simple customizations I use on my own bash prompt.
My prompt displays my username, the current directory’s file path, and the git branch I’m currently in.
To make your bash prompt do this, open your .bashrc file.
$ open ~/.bashrc
.bashrc is a shell script that bash runs when you open bash. This example modifies PS1 (the user prompt). To make yours look like mine, add this to the bottom of your .bashrc file (the # comments are optional).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Add this to the bottom of your .bashrc file and run source ~/.bashrc | |
#thanks to | |
#http://www.ibm.com/developerworks/linux/library/l-tip-prompt/ and | |
#http://martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac/ | |
# Git branch in prompt. | |
parse_git_branch() { | |
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' | |
} | |
export PS1="\u \[\e[30;1m\]\w \[\e[0m\]\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ " |
Save the file, return to your terminal and type source ~/.bashrc to apply the changes.
$ source ~/.bashrc
More bash customizations
That’s just the tip of the iceberg, though, and chances are, there’s more you will want to do to your bash prompt.
Here are two articles I found helpful.
- This IBM Developer Works post on customizing bash includes a handy sequence guide and an explanation of how colors work in bash.
- Martin Fitzpatrick has a great guide to customizing terminal on Mac that I adapted for my own prompt customizations, plus helpful explanations of how this stuff works.
- What is the purpose of .bashrc and how does it work?