Coding with Titans

so breaking things happens constantly, but never on purpose

Customized ZSH prompt with Git info

Probably first thing you noticed after installing macOS Catalina is that the default shell changed from bash to zsh. I decided to give it a try. Although there is an easy way to revert it back to bash (as its package is still part of the OS distribution) by invoking following command in terminal:

chsh -s /bin/bash

My hope was, maybe this small update will inevitably influence my usage comfort, performance and maybe, maybe improve my daily routines (as I spend most of the day working in command-line mode). And indeed, it’s a very good shell, nothing to complain more about. It just misses the tiny visual indicator, that I rely on - which is an info about current branch I am sitting on, while navigating GiT repository. Having it, could be really helpful (even, if I have an alias defined as git b to display name of the branch).

Thankfully, it’s possible and extremely easy to update the prompt. Full guide with nice step-by-step explanation could be found here along with the official docs.

In my condensed essentials, I put all necessary stuff on top of my ~/.zshrc file, which are:

  1. Load info about Version Control System (vcs)
  2. Set the styling, when GiT repository is detected to display branch name in brackets
  3. Updated the prompt respectively

So it looks like following:

# Git branch in prompt

# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }

# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git*' formats '(%b) '

# Set up the prompt (with git branch name)
setopt PROMPT_SUBST
PROMPT='%n@%m %T %~ %B${vcs_info_msg_0_}%b> '
# for current dir only %1d instead of %~

Bonus

There also exists a package manager (called Oh My Zsh), able to add even more fantastic features. Take a look at its plugins section to notice a big collection of TAB-completions and shortcuts. And if at the end you are not happy about it, uninstall it using command: uninstall_oh_my_zsh.

Have fun!