Show current branch on prompt on zsh shell
Asked Answered
N

4

7

I am trying to display the git branch on prompt on Big Sur. So I have created a script file to run for each new session .zshrc

# Git branch in prompt.
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ [\1]/'
}
export PS1="\u@\h \W\[\033[01;33m\]\$(parse_git_branch)\[\033[00m\] $ "

The above doesn't work it actually displays the string not its outpupt.

\u@\h \W\[\033[01;33m\]$(parse_git_branch)\[\033[00m\] $

How can I show current branch in prompt on zsh shell?

Nemathelminth answered 18/5, 2021 at 13:41 Comment(3)
Your post has only statements. This is a Q&A - "Question & Answer" forum. Is there a question you want to ask? Please read How to Ask.Bejewel
@PetrosKalafatidis : \u has no special meaning in a PS1 for zsh. Therefore it is printed literally. The same applies for your other elements in the prompt string.Eadwine
Does this answer your question? How to you configure the command prompt in Linux to show current directory?Eadwine
O
12

I am assuming you want to display something like this [username@computername directory](branch)

The code below should do it for Zsh which is a little more involved than accomplishing the same task in bash. There are multiple solutions to this, and you can find more information here if you are interested.

Add the following to your .zshrc or .zsh_profile

# 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 %1~]%F{green}(${vcs_info_msg_0_})%F{white}$ '

It would be beneficial to read up on the differences between bash and zsh as you cannot use solutions found online interchangeably.

Oteliaotero answered 20/5, 2021 at 22:55 Comment(3)
This works perfectly. I slightly adjusted to change the color and punctuation. PROMPT='%n@%m %1~%F{cyan} (${vcs_info_msg_0_})%F{white} % 'Distributee
It was not obvious to me that the above must be added to your .zshrc file in order to be permanent. Hopefully this helps others. The article linked to in this answer is helpful as well.Distributee
The problem though is that the branch's name appears only if you source the file. If you close the shell and start a new one, everything in the prompt message gets evaluated except for the VCS info. Apparently you'd have to load them manually.Rundell
P
7

I was looking for a script that would do the same on MacOS but didn't find a good answer so I just built it looking up zsh a little bit.

The other answers are right that you the answer is that you're using bash symbols instead of zsh symbols however below is a solution I made.

Add the following to your ~/.zshrc or ~/zprofile

###
# ADD GIT INFO TO PROMPT
###
parse_git_branch() {
  local branch=""
  branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
  local git_status=$(git status --porcelain 2>/dev/null)
  local color=green
  if echo "$git_status" | grep -q "^ M"; then
    color=yellow
    branch="${branch}*"
  fi
  if echo "$git_status" | grep -qE "^ A|^\?\?"; then
    color=yellow
    branch="${branch}+"
  fi
  if echo "$git_status" | grep -q "^ D"; then
    color=yellow
    branch="${branch}-"
  fi

  if [[ -n "$branch" ]]; then
    branch=[%F{${color}}${branch}%F{reset}]
  fi
  echo "$branch"
}
update_prompt() {
    PS1="%n %1~$(parse_git_branch) %#"
}
precmd_functions+=(update_prompt)
update_prompt

This will label your branch and give a little mark if there's changes. If you don't need the mark, only keep the first line in parse_git_branch

If you need the computer name change %n to %n@%m.

Remember to re-open the terminal or run source ~/.zshrc after you make your changes.

Potshot answered 24/2, 2023 at 21:27 Comment(0)
B
0

Bash PS1 is not ZSH PS1, they differ. The \u \h etc. sequences are substituted by bash, they do not work in ZSH. Research both shells and how they differ.

Bejewel answered 18/5, 2021 at 13:49 Comment(0)
D
0

As for me next is the best for MAC

###
# ADD GIT INFO TO PROMPT
###
# Init color
autoload -U colors && colors

# Git branch info    
git_info() {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return
    echo "(${ref#refs/heads/})"
}

# Precmd is executed just before each prompt
precmd() {
    PS1="%{$fg[blue]%}%~ %{$fg[yellow]%}$(git_info)
%{$fg[green]%}%n@%m%{$restore_color%} %# "
}
Deanadeanda answered 4/10 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.