schubam.com

Enabling git info in your zsh prompt

Background

I recently got a new Mac and I wanted to keep configuration and installation of tools basic and to a minimum. I want to leverage as much as I can from already installed tools.

Zsh is the default shell on Macs as of macOS Catalina (10.15), which makes me happy as I have been using zsh with oh-my-zsh for a long time now.

Why not oh-my-zsh?

Even though I have been using oh-my-zsh for about ten years now, I did not use much of its functionality. A bit of tab completion here, a bit of aliases there, but now, I want to try to go as long as I can without installing it.

It has a penalty on shell start time, as it loads many files. It ships a lot of features and themes I don't want, use or need.

Git info in zsh prompt

Here is what I had to do in order to enabling git information in your zsh prompt.

### Start git prompt
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git

() {
    zstyle ':vcs_info:*:*' formats           "(%b%c%u) "
    zstyle ':vcs_info:*:*' actionformats     "(%b%c%u|%a) "
    zstyle ':vcs_info:*:*' stagedstr         "*"
    zstyle ':vcs_info:*:*' unstagedstr       "*"
    zstyle ':vcs_info:*:*' check-for-changes true
}

autoload -Uz add-zsh-hook
add-zsh-hook precmd vcs_info

setopt promptsubst
### End git prompt

PROMPT='%B%F{blue}[%3~]%b%f ${vcs_info_msg_0_}# '

It's very simple and basic. What you see is

  • In blue, I have chosen to show 3 levels of directory tree going up from the current working directory
  • In parenthesis you see the current working branch. The asterisk marks uncommited or unstaged changes (I don't make a difference, but you could).

© Copyright 2020 schubam