Simplest ZSH Prompt Configs for Git Branch Name
Minimal .zshrc examples to include git branch name in prompt
Goal
Configure the zsh shell prompt (default on macOS Catalina) to spit out your git branch name if you are inside a git repository with minimal effort so you can just move on to something else.
By minimal I mean some combination of least additional downloads, least dependency on other tools / programs and least configuration while still being simple enough to read, understand and adjust.
TLWR
- Go to the either ‘Config’ section of this post and copy the code.
- Paste into your
~/.zshrc
file and save. - Start a new shell or
source ~.zshrc
. - Done. Forget about it. Move on with whatever you were doing before.
Context
I’ve recently started working on a MacBook Pro with macOS Catalina which ships with Z Shell instead of Bash as the default shell.
Though a lot feels the same as other MacBooks I’ve used there are some little differences which need to be accommodated.
- The config file is
~/.zshrc
instead of~/.bashrc
. Profiles can also be used (search rc vs profile for context). - Different commands and options can be used to configure the prompt which are only applicable to one shell. E.g using
setopt
in zsh for options. - The new OS and point in time can mean new versions of other utilities and dependencies which may be different than before. For example (and this isn’t specific to this OS update) many online examples make use of the function
__git_ps1
which would be available by default when you install git but this has been moved and now needs to be sourced differently[0].
Note that you are still able to set Bash as the default shell and you can update to a version you want if you wish. I think sticking to zsh is probably the path of least resistance if Apple are now opting for it.
Config 0
Output
It outputs your current folder and the parent directory along with the git branch and some other basic decoration.
This example was informed primarily by this post:
If you want some more information on common prompt configs like username, host, path and colours then check out this post:
Config 1
You can also use the zsh vcs_info
module to obtain the git information. This example will enable the exact same prompt output as Config 0. This is slightly less readable (IMO) but makes it a lot easier to obtain and use various other information from git to use in your prompt if you wish.
For an example of some basic additional config options using this module you can learn from this post:
You can also read the zsh docs for this[1].
Oh My Zsh
This seems to be the right tool for you and I to look into for various jazzy zsh configurations with more features and options with not much additional effort but was outside of the scope of this task.
Resources
[0] https://stackoverflow.com/questions/12870928/mac-bash-git-ps1-command-not-found
[1] http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#Version-Control-Information
A good post series on “moving to zsh”: https://scriptingosx.com/2019/06/moving-to-zsh/
Z Shell Wikipedia: https://en.wikipedia.org/wiki/Z_shell
ZSH Website: https://www.zsh.org/
ZSH Source and docs: http://zsh.sourceforge.net/