Tanner Mann
Tanner Mann

Reputation: 522

How can I modify my zsh terminal prompt to show the current working directory and current git branch (if applicable)?

I am running macOS Big Sur v11.2. I would like my zsh terminal prompt to look like this:

/directoryName/ (gitbranch) $

Thank you!

Upvotes: 0

Views: 1393

Answers (1)

Gairfowl
Gairfowl

Reputation: 2981

One way is use a zsh precmd to build the prompt with the vcs_info builtin. Try adding this to ~/.zshrc and restarting the terminal session:

my_precmd() {
  vcs_info
  psvar[1]=$vcs_info_msg_0_
  if [[ -n ${psvar[1]} ]]; then
    psvar[1]=" (${psvar[1]})"
  fi
}

autoload -Uz vcs_info
zstyle ':vcs_info:git:*' formats '%b'
autoload -Uz add-zsh-hook
add-zsh-hook precmd my_precmd
PROMPT='%d/%1v $ '

Some notes about the pieces are here: Different zsh terminal prompt when outside git directory

Upvotes: 2

Related Questions